Handle Access-Denied while moving files to remote computer

This topic contains 1 reply, has 2 voices, and was last updated by  Don Jones 2 years, 8 months ago.

  • Author
  • #29125

    Could you please help me to validate this.
    Scenario: I am trying move files from a shared path to remote machine.In that situation i am doing Test-Connection to check whether server is pingable or not.If Test-Connection returns true i am using Move-Item to move files. But in this case even though server is pingable,i dont have acces to that server to login. So Move-Item fails with message "Access-Denied". Could you please help me to validate this.

    If i dont have an access, simply i should log those details and need to move to next instead of displaying those error on the screen like below.

    PS H:\> Move-Item -Path D:\Test.txt -Destination \\ServerName\D$\Test.txt -Force
    Move-Item : Access is denied
    At line:1 char:1
    + Move-Item -Path D:\Test.txt -Destination \\ServerName\D$\Test.txt -Force
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : PermissionDenied: (D:\Test.txt:String) [Move-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : MoveItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.MoveItemCommand
    Even i tried to catch this in Catch block like below but its not helping me.
    Catch [System.UnauthorizedAccessException]
    write-host "Not authorized to perform this operation"

  • #29126

    Don Jones

    Consider reading The Big Book of PowerShell Error Handling. It's free on our Resouces / ebooks menu and has a lot of detail about dealing with errors.

You must be logged in to reply to this topic.