PowerShell路径存在但脚本提示找不到,远程文件传输失败求助
问题原因与解决方案
核心问题:PowerShell远程会话的双跳限制
你遇到的报错是典型的Kerberos双跳问题:本地机器能访问NAS(所以Test-Path返回True),但通过Invoke-Command发起的远程会话无法继承你的本地凭据去访问NAS,导致远程服务器上的Copy-Item命令找不到NAS路径。目标服务器本地执行成功是因为此时用的是服务器本地的上下文(有权限访问NAS)。
解决方法
方法1:启用CredSSP凭据委派(推荐,适合域环境)
这种方法允许将你的本地凭据转发到远程服务器,让远程会话能访问NAS:
- 在本地执行(需管理员权限):
Enable-WSManCredSSP -Role Client -DelegateComputer "*.yourdomain.com" # 替换成你的域后缀,或者指定具体的远程服务器名 - 在所有目标远程服务器上执行(需管理员权限):
Enable-WSManCredSSP -Role Server - 修改你的脚本,添加
-Authentication CredSSP参数:$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 -Authentication CredSSP }
方法2:本地中转文件(无需域权限配置)
先把NAS文件复制到本地临时目录,再从本地推送到远程服务器:
$sourceFile = "\\rcnas\foo\bar\sample.txt" $localTempFile = "$env:TEMP\sample.txt" $remotePath = "D:\OPS\" # 先将NAS文件拉到本地 Copy-Item -Path $sourceFile -Destination $localTempFile -Force foreach ($serverName in $servers) { Invoke-Command -ComputerName $serverName -ScriptBlock { Copy-Item -Path $using:localTempFile -Destination $using:remotePath -Force } -Credential $cred } # 清理本地临时文件 Remove-Item -Path $localTempFile -Force
方法3:用远程服务器的本地权限访问NAS
如果目标服务器的本地账户(或域账户)本身有NAS访问权限,可以在远程会话中直接使用该账户的上下文访问NAS:
$sourceFile = "\\rcnas\foo\bar\sample.txt" $remotePath = "D:\OPS\" foreach ($serverName in $servers) { Invoke-Command -ComputerName $serverName -ScriptBlock { # 输入有权访问NAS的凭据 $nasCred = Get-Credential -Message "输入NAS访问凭据" Copy-Item -Path $using:sourceFile -Destination $using:remotePath -Force -Credential $nasCred } -Credential $cred }
内容的提问来源于stack exchange,提问作者Yashbhatt
相关产品推荐
相关产品推荐

