This topic contains 4 replies, has 4 voices, and was last updated by
July 29, 2014 at 6:29 am #17563ParticipantPoints: 0Rank: Member
I have a powershell script that runs from the local machine, creates a txt file on the local machine and then queries a remote machine to get various info back on that status of that remote machine (cpu, ram usage etc) All that works fine.
I then want to write that data to a text on the local machine but I'm having Access Denied issues.
The line I'm using is:-
Add-Content "\\192.168.0.72\e$\Domains\domain.com\wwwroot\servers\txt\$ComputerName.txt" $line -force
running it gives me the following error:-
Access to the path '192.168.0.72\e$\domains\domain.com\wwwroot\servers\txt\servername.txt is denied, Category Info : PermissionDenied
The txt file is valid and on the local machine and the $ComputerName var is populated correctly
It's running under an administrator account that is present on both the remote and the local machine
Anyone got any ideas what the problem is?
July 29, 2014 at 6:34 am #17564MemberPoints: 0Rank: Member
It's hard to tell from the context here, but it looks like you're using Invoke-Command to run this script on a remote computer, then trying to write back to your local computer from there. If so, you're hitting the "second hop" problem of Remoting, and there are many threads and resources describing how to work around that (including the free "Secrets of PowerShell Remoting" eBook on this site.) However, in this case, it's probably better just to change your code a bit.
If you're using Invoke-Command, there's no reason you need to output to a file from inside the remote session; any objects written to the Output stream are going to be serialzed and returned to your local computer anyway. Then you can handle the file output locally. Also, if you're using WMI or CIM to fetch the system information you referenced, then you may not even need Invoke-Command at all, since WMI / CIM work remotely anyway.
July 29, 2014 at 6:39 am #17565ParticipantPoints: 638Rank: Major Contributor
First, I would take the script out of the equation. Can you do what you are attempting to do manually with Notepad? You mentioned that a script creates the file. What methods does the script use to write content to the file? A common reason for "Access Denied" is because something has the file open for writing already, so you cannot write to the file.
July 29, 2014 at 6:46 am #17566KeymasterPoints: 1,811Rank: Community Hero
Agree with Dave, and to perhaps offer a more concise version: if you're asking a remote machine to access a UNC, it'll fail unless you've accounted for authentication delegation. Either let your local machine do the writing, or tell the remote machine to use a local path (E:, for example).
July 30, 2014 at 1:35 am #17606ParticipantPoints: 0Rank: Member
Thanks guys – with a bit of tweaking it all works correctly now. I appreciate the help.
$AllContent = Invoke-Command -Session $Session -ScriptBlock $script -ArgumentList $WebServerIP, $ComputerName, $str_AccountNumber, $int_UserId
Add-Content "e:\domains\domain.com\wwwroot\servers\txt\$ComputerName.txt" $AllContent -force
The topic ‘Access Denied’ is closed to new replies.