admin管理员组

文章数量:1360358

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.

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.

Share Improve this question asked Apr 1 at 6:44 YashbhattYashbhatt 1115 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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

本文标签: windowsPowershell path exists but script cannot find itStack Overflow