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.
1 Answer
Reset to default 2I 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
版权声明:本文标题:windows - Powershell path exists but script cannot find it - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743904487a2559270.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论