Access Denied

This topic contains 4 replies, has 4 voices, and was last updated by Profile photo of Paul Hopkinson Paul Hopkinson 2 years, 4 months ago.

  • Author
    Posts
  • #17563
    Profile photo of Paul Hopkinson
    Paul Hopkinson
    Participant

    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:-
    [blockquote]Add-Content "\\192.168.0.72\e$\Domains\domain.com\wwwroot\servers\txt\$ComputerName.txt" $line -force[/blockquote]

    running it gives me the following error:-

    [blockquote]Access to the path '192.168.0.72\e$\domains\domain.com\wwwroot\servers\txt\servername.txt is denied, Category Info : PermissionDenied[/blockquote]

    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?

    Thanks

    Paul

  • #17564
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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.

  • #17565
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    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.

  • #17566
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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).

  • #17606
    Profile photo of Paul Hopkinson
    Paul Hopkinson
    Participant

    Thanks guys – with a bit of tweaking it all works correctly now. I appreciate the help.

    Final code:-

    [blockquote]$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[/blockquote]

    Paul

You must be logged in to reply to this topic.