我已经编写了以下PowerShell脚本来进行远程Office卸载。
$computers = @("WGBCZC4253TQY")
foreach ($computer in $computers) {
Copy-Item -Force -Recurse -Verbose -Path \\sgblon340012\O365\Remove-PreviousOfficeInstalls -Destination \\$computer\C$\temp200718\
Invoke-Command -ComputerName $computer -ScriptBlock {
& {
Set-ExecutionPolicy -ExecutionPolicy Bypass -Force -Verbose
cd C:\temp200718\Remove-PreviousOfficeInstalls -Verbose
. .\Remove-PreviousOfficeInstalls.ps1
Remove-PreviousOfficeInstalls
Set-ExecutionPolicy -ExecutionPolicy Bypass -Force -Verbose
}
}
Remove-Item -Force -Recurse \\$computer\C$\temp200718 -Verbose
}
但是它会生成以下关于该路径不存在的错误,即使您可以清楚地看到该路径确实存在,因为文件刚刚被复制!
VERBOSE: Performing the operation "Copy Directory" on target "Item: \\sgblon340012\O365\Remove-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\Remove-PreviousOfficeInstalls\OffScrub03.vbs Destination: \\WGBCZC4253TQY\C$\temp200718\OffScrub03.vbs". VERBOSE: Performing the operation "Copy File" on target "Item: \\sgblon340012\O365\Remove-PreviousOfficeInstalls\OffScrub07.vbs Destination: \\WGBCZC4253TQY\C$\temp200718\OffScrub07.vbs". VERBOSE: Performing the operation "Copy File" on target "Item: \\sgblon340012\O365\Remove-PreviousOfficeInstalls\OffScrub10.vbs Destination: \\WGBCZC4253TQY\C$\temp200718\OffScrub10.vbs". VERBOSE: Performing the operation "Copy File" on target "Item: \\sgblon340012\O365\Remove-PreviousOfficeInstalls\OffScrubc2r.vbs Destination: \\WGBCZC4253TQY\C$\temp200718\OffScrubc2r.vbs". VERBOSE: Performing the operation "Copy File" on target "Item: \\sgblon340012\O365\Remove-PreviousOfficeInstalls\OffScrub_O15msi.vbs Destination: \\WGBCZC4253TQY\C$\temp200718\OffScrub_O15msi.vbs". VERBOSE: Performing the operation "Copy File" on target "Item: \\sgblon340012\O365\Remove-PreviousOfficeInstalls\OffScrub_O16msi.vbs Destination: \\WGBCZC4253TQY\C$\temp200718\ OffScrub_O16msi.vbs". VERBOSE: Performing the operation "Copy File" on target "Item: \\sgblon340012\O365\Remove-PreviousOfficeInstalls\README.md Destination: \\WGBCZC4253TQY\C$\temp200718\README.md". VERBOSE: Performing the operation "Copy File" on target "Item: \\sgblon340012\O365\Remove-PreviousOfficeInstalls\README_Remove-PreviousOfficeInstalls.md Destination: \\WGBCZC4253TQY\C$\temp200718\README_Remove-PreviousOfficeInstalls.md". VERBOSE: Performing the operation "Copy File" on target "Item: \\sgblon340012\O365\Remove-PreviousOfficeInstalls\Remove-PreviousOfficeInstalls.ps1 Destination: \\WGBCZC4253TQY\C$\temp200718\Remove-PreviousOfficeInstalls.ps1". VERBOSE: Performing operation "Set-ExecutionPolicy" on Target "Bypass". Cannot find path 'C:\temp200718\Remove-PreviousOfficeInstalls' because it does not exist. + CategoryInfo : ObjectNotFound: (C:\temp200718\R...sOfficeInstalls:String) [Set-Location], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand + PSComputerName : WGBCZC4253TQY The term '.\Remove-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: (.\Remove-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\README.md". VERBOSE: Performing the operation "Remove File" on target "\\WGBCZC4253TQY\C$\temp200718\README_Remove-PreviousOfficeInstalls.md". VERBOSE: Performing the operation "Remove File" on target "\\WGBCZC4253TQY\C$\temp200718\Remove-PreviousOfficeInstalls.ps1".
答案 0 :(得分:0)
这是PowerShell陷阱。如果将文件夹复制到不存在的目标文件夹,PowerShell将创建目标文件夹,然后复制源文件夹的 content 。如果要将整个源文件夹复制到目标文件夹,请确保目标文件夹已存在:
$src = '\\sgblon340012\O365\Remove-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
或者,您可以指定完整的目标路径:
$src = '\\sgblon340012\O365\Remove-PreviousOfficeInstalls'
$dst = "\\${computer}\C$\temp200718\Remove-PreviousOfficeInstalls"
Copy-Item -Path $src -Destination "${dst}\" -Force -Recurse -Verbose
但是,由于某种原因该目标文件夹应该已经存在,该命令会将整个源文件夹复制到目标文件夹,从而创建路径\\${computer}\C$\temp200718\Remove-PreviousOfficeInstalls\Remove-PreviousOfficeInstalls\
。因此,我不建议您选择此路线。坚持第一个例子。