Access Denied

Welcome Forums General PowerShell Q&A Access Denied

This topic contains 4 replies, has 4 voices, and was last updated by

4 years, 7 months ago.

  • Author
  • #17563

    Points: 0
    Rank: 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 "\\\e$\Domains\\wwwroot\servers\txt\$ComputerName.txt" $line -force

    running it gives me the following error:-

    Access to the path '\e$\domains\\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?



  • #17564

    Points: 0
    Rank: 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.

  • #17565

    Points: 638
    Helping Hand
    Rank: 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.

  • #17566

    Points: 1,811
    Helping HandTeam Member
    Rank: 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).

  • #17606

    Points: 0
    Rank: Member

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

    Final code:-

    $AllContent = Invoke-Command -Session $Session -ScriptBlock $script -ArgumentList $WebServerIP, $ComputerName, $str_AccountNumber, $int_UserId

    Add-Content "e:\domains\\wwwroot\servers\txt\$ComputerName.txt" $AllContent -force


The topic ‘Access Denied’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort