1

I am trying to use a PowerShell script to transfer a file from a NAS location to a group of remote servers. I am using Copy-Item to transfer the file. I am getting the error - Cannot find path because it does not exist.

However, when I execute Test-Path in the shell, it returns True. In addition, if I run the command on a server from that group locally, it successfully copies it.

$sourceFile = "\\rcnas\foo\bar\sample.txt"
$remotePath = "D:\OPS\"

foreach ($serverName in $servers) {
    Invoke-Command -ComputerName $serverName -ScriptBlock {
        Copy-Item -Path $using:sourceFile -Destination $using:remotePath -Force
    } -Credential $cred
}

I tried doing it using PSSession and also by adding params and ArgumentList but still facing the same issue.

1 Answer 1

2

I am getting the error - Cannot find path because it does not exist.

However, when I execute Test-Path in the shell, it returns True.

This is likely a credential hopping issue - there are no forwardable credentials available in the remoting session to authenticate the transactions against the \\rcnas\foo share, hence the error.

The easiest option (which may or may not be appropriate depending on the volume of data you need to transfer) is to establish a remoting session on the target machine and then use Copy-Item's -ToSession parameter to perform remote copy:

foreach ($serverName in $servers) {
    $session = New-PSSession -ComputerName $serverName -Credential $cred
    Copy-Item -ToSession $session -Path $sourceFile -Destination $remotePath -Force 
}

You might want to copy the file(s) from the NAS to the executing machine once up front to avoid re-reading the same data off the share every time

Sign up to request clarification or add additional context in comments.

1 Comment

This worked but I do not understand why. The mentioned NAS path does not require credentials or admin access to copy the files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.