Remote script permissions issue

I have a simple PowerShell script which copies files to a backup share. If I run this command locally, the files copy without issue. However, if I run the script from a remote computer under the same account, all steps in the script succeed until the Copy-Item command which returns “Access is denied”.

The script is being called remotely through a command of the form:

Invoke-Command -ComputerName RemoteServer -ScriptBlock {& "E:\Scripts\CopyScript.ps1" -FolderName "\\FileServer\BackupFolder"}

I have added a “Start-Transcript” command to the script and this returns the correct account name in the header but the Copy-Item fails.

Is this a double-hop authentication issue or am I missing something else? If it is a double-hop issue, is there a way around this? Note that granting “Full Control” to the target folder and target share for “Everyone” doesn’t solve the problem.

Thanks.

This is totally the double hop problem, and the credentials need to be passed into the remote computer.

Since you’re dealing with file activities, something like this should work:

$cred = get-credential

Invoke-Command -ComputerName $machineA -ScriptBlock {
    New-PSDrive -Name X -PSProvider FileSystem -Root "\\FileServer\BackupFolder" -Credential $using:cred
    & "E:\Scripts\CopyScript.ps1" -FolderName "X:\"
}

2 Likes

Thanks for that solution. I’d never have come up with that!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.