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.