Reputation: 33
I have written the below PowerShell script to do a remote Office uninstall.
$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
}
but it generates the below errors about the path not existing even though you can clearly see that it does exist as the files have just been copied!
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".
Upvotes: 3
Views: 31752
Reputation: 200193
This is a PowerShell gotcha. If you copy a folder to a non-existing destination folder, PowerShell will create the destination folder and then copy the content of the source folder. If you want the entire source folder copied to the destination folder, make sure the destination folder already exists:
$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
Alternatively you could specify the full destination path:
$src = '\\sgblon340012\O365\Remove-PreviousOfficeInstalls'
$dst = "\\${computer}\C$\temp200718\Remove-PreviousOfficeInstalls"
Copy-Item -Path $src -Destination "${dst}\" -Force -Recurse -Verbose
However, should that destination folder already exist for some reason the command will copy the entire source folder to the destination folder, thus creating a path \\${computer}\C$\temp200718\Remove-PreviousOfficeInstalls\Remove-PreviousOfficeInstalls\
. Hence I don't recommend going this route. Stick with the first example.
Upvotes: 2