Cannot find path because it doesn't exist
我已经编写了以下PowerShell脚本来进行远程Office卸载。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | $computers = @("WGBCZC4253TQY") foreach ($computer in $computers) { Copy-Item -Force -Recurse -Verbose -Path \\\\sgblon340012\\O365\ emove-PreviousOfficeInstalls -Destination \\\\$computer\\C$\\temp200718\\ Invoke-Command -ComputerName $computer -ScriptBlock { & { Set-ExecutionPolicy -ExecutionPolicy Bypass -Force -Verbose cd C:\\temp200718\ emove-PreviousOfficeInstalls -Verbose . .\ emove-PreviousOfficeInstalls.ps1 Remove-PreviousOfficeInstalls Set-ExecutionPolicy -ExecutionPolicy Bypass -Force -Verbose } } Remove-Item -Force -Recurse \\\\$computer\\C$\\temp200718 -Verbose } |
但是它会生成以下关于该路径不存在的错误,即使您可以清楚地看到该路径确实存在,因为文件已被复制!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | VERBOSE: Performing the operation"Copy Directory" on target"Item: \\\\sgblon340012\\O365\ emove-PreviousOfficeInstalls Destination: \\\\WGBCZC4253TQY\\C$\\temp200718". VERBOSE: Performing the operation"Create Directory" on target"Destination: \\\\WGBCZC4253TQY\\C$\\temp200718". VERBOSE: Performing the operation"Copy File" on target"Item: \\\\sgblon340012\\O365\ emove-PreviousOfficeInstalls\\OffScrub03.vbs Destination: \\\\WGBCZC4253TQY\\C$\\temp200718\\OffScrub03.vbs". VERBOSE: Performing the operation"Copy File" on target"Item: \\\\sgblon340012\\O365\ emove-PreviousOfficeInstalls\\OffScrub07.vbs Destination: \\\\WGBCZC4253TQY\\C$\\temp200718\\OffScrub07.vbs". VERBOSE: Performing the operation"Copy File" on target"Item: \\\\sgblon340012\\O365\ emove-PreviousOfficeInstalls\\OffScrub10.vbs Destination: \\\\WGBCZC4253TQY\\C$\\temp200718\\OffScrub10.vbs". VERBOSE: Performing the operation"Copy File" on target"Item: \\\\sgblon340012\\O365\ emove-PreviousOfficeInstalls\\OffScrubc2r.vbs Destination: \\\\WGBCZC4253TQY\\C$\\temp200718\\OffScrubc2r.vbs". VERBOSE: Performing the operation"Copy File" on target"Item: \\\\sgblon340012\\O365\ emove-PreviousOfficeInstalls\\OffScrub_O15msi.vbs Destination: \\\\WGBCZC4253TQY\\C$\\temp200718\\OffScrub_O15msi.vbs". VERBOSE: Performing the operation"Copy File" on target"Item: \\\\sgblon340012\\O365\ emove-PreviousOfficeInstalls\\OffScrub_O16msi.vbs Destination: \\\\WGBCZC4253TQY\\C$\\temp200718\\ OffScrub_O16msi.vbs". VERBOSE: Performing the operation"Copy File" on target"Item: \\\\sgblon340012\\O365\ emove-PreviousOfficeInstalls\ EADME.md Destination: \\\\WGBCZC4253TQY\\C$\\temp200718\ EADME.md". VERBOSE: Performing the operation"Copy File" on target"Item: \\\\sgblon340012\\O365\ emove-PreviousOfficeInstalls\ EADME_Remove-PreviousOfficeInstalls.md Destination: \\\\WGBCZC4253TQY\\C$\\temp200718\ EADME_Remove-PreviousOfficeInstalls.md". VERBOSE: Performing the operation"Copy File" on target"Item: \\\\sgblon340012\\O365\ emove-PreviousOfficeInstalls\ emove-PreviousOfficeInstalls.ps1 Destination: \\\\WGBCZC4253TQY\\C$\\temp200718\ emove-PreviousOfficeInstalls.ps1". VERBOSE: Performing operation"Set-ExecutionPolicy" on Target"Bypass". Cannot find path 'C:\\temp200718\ emove-PreviousOfficeInstalls' because it does not exist. + CategoryInfo : ObjectNotFound: (C:\\temp200718\ ...sOfficeInstalls:String) [Set-Location], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand + PSComputerName : WGBCZC4253TQY The term '.\ emove-PreviousOfficeInstalls.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. + CategoryInfo : ObjectNotFound: (.\ emove-PreviousOfficeInstalls.ps1:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException + PSComputerName : WGBCZC4253TQY The term 'Remove-PreviousOfficeInstalls' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. + CategoryInfo : ObjectNotFound: (Remove-PreviousOfficeInstalls:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException + PSComputerName : WGBCZC4253TQY VERBOSE: Performing operation"Set-ExecutionPolicy" on Target"Bypass". VERBOSE: Performing the operation"Remove Directory" on target"\\\\WGBCZC4253TQY\\C$\\temp200718". VERBOSE: Performing the operation"Remove File" on target"\\\\WGBCZC4253TQY\\C$\\temp200718\\OffScrub03.vbs". VERBOSE: Performing the operation"Remove File" on target"\\\\WGBCZC4253TQY\\C$\\temp200718\\OffScrub07.vbs". VERBOSE: Performing the operation"Remove File" on target"\\\\WGBCZC4253TQY\\C$\\temp200718\\OffScrub10.vbs". VERBOSE: Performing the operation"Remove File" on target"\\\\WGBCZC4253TQY\\C$\\temp200718\\OffScrubc2r.vbs". VERBOSE: Performing the operation"Remove File" on target"\\\\WGBCZC4253TQY\\C$\\temp200718\\OffScrub_O15msi.vbs". VERBOSE: Performing the operation"Remove File" on target"\\\\WGBCZC4253TQY\\C$\\temp200718\\OffScrub_O16msi.vbs". VERBOSE: Performing the operation"Remove File" on target"\\\\WGBCZC4253TQY\\C$\\temp200718\ EADME.md". VERBOSE: Performing the operation"Remove File" on target"\\\\WGBCZC4253TQY\\C$\\temp200718\ EADME_Remove-PreviousOfficeInstalls.md". VERBOSE: Performing the operation"Remove File" on target"\\\\WGBCZC4253TQY\\C$\\temp200718\ emove-PreviousOfficeInstalls.ps1". |
这是PowerShell陷阱。如果将文件夹复制到不存在的目标文件夹,PowerShell将创建目标文件夹,然后复制源文件夹的内容。如果要将整个源文件夹复制到目标文件夹,请确保目标文件夹已存在:
1 2 3 4 5 6 7 | $src = '\\\\sgblon340012\\O365\ emove-PreviousOfficeInstalls' $dst ="\\\\${computer}\\C$\\temp200718" if (-not (Test-Path -LiteralPath $dst)) { New-Item -Type Directory -Path $dst | Out-Null } Copy-Item -Path $src -Destination"${dst}" -Force -Recurse -Verbose |
或者,您可以指定完整的目标路径:
1 2 3 4 5 | $src = '\\\\sgblon340012\\O365\ emove-PreviousOfficeInstalls' $dst ="\\\\${computer}\\C$\\temp200718\ emove-PreviousOfficeInstalls" Copy-Item -Path $src -Destination"${dst}" -Force -Recurse -Verbose |
但是,由于某种原因该目标文件夹应该已经存在,该命令会将整个源文件夹复制到目标文件夹,从而创建路径
emove-PreviousOfficeInstalls\
emove-PreviousOfficeInstalls\\