像这样写的问题很可能会引发“你尝试过什么”类型的回答......
我建议使用 Windows Installer Powershell Module Uninstall-MSIProduct。
我在这篇文章中描述了如何远程使用这个模块:remote PCs using get-msiproductinfo,这个例子使用Get-MSIProductInfo但可以很容易地更新为使用Uninstall-MSIProduct。
我已经快速将其更改为使用Uninstall-MSIProduct,但尚未对其进行测试。
[cmdletbinding()]
param
(
[parameter(Mandatory=$true,ValueFromPipeLine=$true,ValueFromPipelineByPropertyName=$true)]
[string]
$computerName,
[string]
$productCode
)
begin
{
write-verbose "Starting: $($MyInvocation.MyCommand)"
$scriptFolder = Split-Path -Parent $MyInvocation.MyCommand.Path
$moduleName = "MSI"
$modulePath = Join-Path -Path $scriptFolder -ChildPath $moduleName
$remoteScript = {
param($targetPath,$productCode)
Import-Module $targetPath
uninstall-msiproduct -ProductCode $productCode
}
$delayedDelete = {
param($path)
Remove-Item -Path $path -Force -Recurse
}
}
process
{
$remotePath = "\\$computerName\c$\temp\$moduleName"
write-verbose "Copying module to $remotePath"
Copy-Item -Path $modulePath -Destination $remotePath -Recurse -Container -Force
write-verbose "Getting installed products"
Invoke-Command -ComputerName $computerName -ScriptBlock $remoteScript -ArgumentList "c:\temp\$moduleName", $productCode
write-verbose "Starting job to delete $remotePath"
Start-Job -ScriptBlock $delayedDelete -ArgumentList $remotePath | Out-Null
}
end
{
write-verbose "Complete: $($MyInvocation.MyCommand)"
}