Powershell Copy-Item 到远程会话随机失败

Powershell Copy-Item to remote session fails randomly

我正在使用此 Powershell 命令将包含许多子文件夹和文件的文件夹从我的本地计算机复制到远程 VM。

$session = New-PSSession -ComputerName $desComputerName -Credential $credentials
Copy-Item -Path $sourceFolder -Destination $destinationFolder -Recurse -Force -ToSession $session

大多数时候它工作得很好,一切都被复制了,但有时却没有。 这是我在失败时总是得到的错误:

Copy-Item : Failed to create directory <A-Random-Folder> on remote destination.

这是完整的错误日志:

Copy-Item : Starting a command on the remote server failed with the following error message : <s:Envelope 
xml:lang="en-US" xmlns:s="http://www.w3.org/2003/05/soap-envelope" 
xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:x="http://schemas.xmlsoap.org/ws/2004/09/transfer" 
xmlns:e="http://schemas.xmlsoap.org/ws/2004/08/eventing" xmlns:n="http://schemas.xmlsoap.org/ws/2004/09/enumeration" 
xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd" xmlns:p="http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd">
<s:Header><a:Action>http://schemas.dmtf.org/wbem/wsman/1/wsman/fault</a:Action><a:MessageID>uuid:BE54F3C6-3E48-40CC-882
6-29DECA14BAC7</a:MessageID><p:OperationID s:mustUnderstand="false">uuid:03909886-0D49-489B-AD87-AC5654A64257</p:Operat
ionID><p:SequenceId>1</p:SequenceId><a:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:To><a:Rela
tesTo>uuid:82E5942D-D7CC-40B9-B16D-2E2A07635273</a:RelatesTo></s:Header><s:Body><s:Fault><s:Code><s:Value>s:Receiver</s
:Value><s:Subcode><s:Value>w:InternalError</s:Value></s:Subcode></s:Code><s:Reason><s:Text 
xml:lang="en-US"></s:Text></s:Reason><s:Detail><unknown></s:Detail></s:Fault></s:Body></s:Envelope> For more 
information, see the about_Remote_Troubleshooting Help topic.
At MyFolder\Deploy.ps1:63 char:5
+     Copy-Item -Path $sourceFolder -Destination $destinationFolder -Re ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Management.Automation.PowerShell:PowerShell) [Copy-Item], PSRe 
   motingTransportException
    + FullyQualifiedErrorId : CopyFileRemoteExecutionError,Microsoft.PowerShell.Commands.CopyItemCommand

Copy-Item : Failed to create directory 'MyDestinationFolder\CKEditor\plugins\pastefromword\filter' on 
remote destination.
At MySourceFolder\Deploy.ps1:63 char:5
+     Copy-Item -Path $sourceFolder -Destination $destinationFolder -Re ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (MyDestinationFolder\CKEditor\plugins\pastefromword\filter:String) [Copy-Item], IOException
    + FullyQualifiedErrorId : FailedToCreateDirectory,Microsoft.PowerShell.Commands.CopyItemCommand

总结:

一些可能有用也可能没用的想法:

检查目标文件夹和文件的总长度 -- 因为您正在复制“许多文件夹和文件”,您可能会达到限制之一:

240 characters for directory paths

260 characters for file fullnames

另一个想法是在副本上使用 -container 开关,记录为:“指示此 cmdlet 在复制操作期间保留容器对象。”

正如其他人所建议的,如果您可以使用文件共享,请考虑使用 Xcopy 或 Robocopy。

在使用 XCopy 自动构建期间复制千兆字节的深层目录时,我取得了出色的结果。

Robocopy 功能更强大,可以更好地控制重试,但通常不值得为其非常广泛和复杂的开关带来额外的麻烦。