PowerShell Copy-Item复制文件夹内容到SharePoint目录异常求助
问题原因
你当前的代码是将C:\SourceFolder目录本身复制到目标SharePoint路径,而非该目录内的内容。当执行Get-ChildItem -Path C:\SourceFolder时,返回的是SourceFolder这个目录对象,而非其内部的文件和子目录。管道传递给Copy-Item后,会在目标路径下创建同名的SourceFolder子目录,这就是你看到目标路径显示为\SourceFolder的原因。
解决方案
方案1:直接用Copy-Item复制目录内容
无需通过Get-ChildItem管道,直接指定源路径为C:\SourceFolder\*,即可将目录内所有内容复制到目标SharePoint目录:
$SharepointDirectory = '\\Sharepoint\Dir' net use $SharepointDirectory /user:domain\user Password Copy-Item -Path "C:\SourceFolder\*" -Destination $SharepointDirectory -Recurse -Force -WhatIf
方案2:通过Get-ChildItem获取目录内容再复制
如果坚持使用管道方式,需确保Get-ChildItem返回的是SourceFolder内部的内容而非目录本身,可通过在源路径末尾添加反斜杠\实现:
$SharepointDirectory = '\\Sharepoint\Dir' net use $SharepointDirectory /user:domain\user Password $FolderName = "C:\SourceFolder\" Get-ChildItem -Path $FolderName | Copy-Item -Destination $SharepointDirectory -Recurse -Force -WhatIf
验证效果
执行上述任一方案后,WhatIf输出会显示类似如下内容,说明内容正在被直接复制到目标SharePoint目录,而非创建新的SourceFolder子目录:
What if: Performing the operation "Copy File" on target "Item: C:\SourceFolder\file1.txt Destination: \\Sharepoint\Dir\file1.txt". What if: Performing the operation "Copy Directory" on target "Item: C:\SourceFolder\SubFolder Destination: \\Sharepoint\Dir\SubFolder".
内容的提问来源于stack exchange,提问作者Antoine Delplanque
相关产品推荐
相关产品推荐

