我使用以下代码卸载谷歌chrome软件从我的远程机器,但这个脚本执行没有输出。当我检查控制面板时,谷歌浏览器程序仍然存在。有人能检查一下这个代码吗?
foreach($computer in (Get-Content pathtothefile))
{
$temp1 = Get-WmiObject -Class Win32_Product -ComputerName $computer | where { $_.name -eq "Google Chrome"}
$temp1.Uninstall()
}
您不应该使用Win32_Product
WMI类,枚举操作的副作用之一是检查每个已安装程序的完整性,如果完整性检查失败,则执行修复安装。
查询注册表获取此信息更安全,该信息还恰好包含用于使用msiexec
删除产品的卸载字符串。此处的卸载字符串将格式化为MsiExec.exe /X{PRODUCT_CODE_GUID}
,PRODUCT_CODE_GUID
将替换为该软件的实际产品代码。
注意:此方法仅适用于安装了MSI安装程序的产品(或提取和安装MSI的安装可执行文件)。对于不使用MSI安装的纯可执行安装程序,您需要查阅产品文档以了解如何卸载该软件,并找到另一种方法(例如知名的安装位置)来确定该软件是否已安装。
注2:我不确定这是什么时候改变的,但
ChromeSetup.exe
不再像以前那样包装MSI。我已经修改了下面的代码来处理删除安装了msi和exe的Chrome版本。
# We need to check both 32 and 64 bit registry paths
$regPaths =
"HKLM:SOFTWAREWow6432nodeMicrosoftWindowsCurrentVersionUninstall",
"HKLM:SOFTWAREMicrosoftWindowsCurrentVersionUninstall"
# Since technically you could have 32 and 64 bit versions of the same
# software, force $uninstallStrings to be an array to cover that case
# if this is reused elsewhere. Chrome usually should only have one or the
# other, however.
$productCodes = @( $regPaths | Foreach-Object {
Get-ItemProperty "${_}*" | Where-Object {
$_.DisplayName -eq 'Google Chrome'
}
} ).PSPath
# Run the uninstall string (formatted like
$productCodes | ForEach-Object {
$keyName = ( Get-ItemProperty $_ ).PSChildName
# GUID check (if the previous key was not a product code we'll need a different removal strategy)
if ( $keyName -match '^{[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}}$' ) {
# Use Start-Process with -Wait to wait for PowerShell to finish
# /qn suppresses the prompts for automation
$p = Start-Process -Wait msiexec -ArgumentList "/l*v ""$($pwd.FullName)/chromeuninst.log"" /X${keyName} /qn" -PassThru
# 0 means success, but 1638 means uninstallation is pending a reboot to complete
# This should still be considered a successful execution
$acceptableExitCodes = 0, 1638
}
else {
Write-Host "Stopping all running instances of chrome.exe, if any are running"
Get-Process chrome.exe -EA Ignore | Stop-Process -Force
# The registry key still has an uninstall string
# But cannot be silently removed
# So we will have to get creating and control the uninstall window with PowerShell
# We need to use the undocumented --force-uninstall parameter, added to below command
$uninstallString = "$(( Get-ItemProperty $_).UninstallString )) --force-uninstall"
# Break up the string into the executable and arguments so we can wait on it properly with Start-Process
$firstQuoteIdx = $uninstallString.IndexOf('"')
$secondQuoteIdx = $uninstallString.IndexOf('"', $firstQuoteIdx + 1)
$setupExe = $uninstallString[$firstQuoteIdx..$secondQuoteIdx] -join ''
$setupArgs = $uninstallString[( $secondQuoteIdx + 1 )..$uninstallString.Length] -join ''
Write-Host "Uninstallation command: ${setupExe} ${setupArgs}"
$p = Start-Process -Wait -FilePath $setupExe -ArgumentList $setupArgs -PassThru
# My testing shows this exits on exit code 19 for success. However, this is undocumented
# behavior so you may need to tweak the list of acceptable exit codes or remove this check
# entirely.
#
$acceptableExitCodes = 0, 19
}
if ( $p.ExitCode -notin $acceptableExitCodes ) {
Write-Error "Program exited with $($p.ExitCode)"
$p.Dispose()
exit $p.ExitCode
}
exit 0
}
顺便说一下,如果您已经知道给定程序的MSI ProductCode,则不必以这种方式找到卸载字符串。您可以简单地执行msiexec /X{PRODUCT_CODE_GUID}
。
如果您有进一步的问题,而不是由上述语法引起的,这将是一个操作问题,最好在https://superuser.com站点上进行故障排除。
编辑
通过我们的聊天对话发现,您正在安装每个用户和79.0.3945.130
版本。您可以使用以下命令删除每个用户的Chrome,如果每个用户安装(如果版本不同,您将需要正确的版本路径):
&"C:UsersusernameAppDataLocalGoogleChromeApplication79.0.3945.130Installersetup.exe" --uninstall --channel=stable --verbose-logging --force-uninstall
在未来,不建议在企业环境中使用ChromeSetup.exe
或ChromeStandaloneSetup64.exe
来安装和管理Chrome,您应该使用企业MSI并安装系统范围的Chrome,以便您可以更有效地管理Chrome。这是在企业环境中部署Chrome的支持方式,我提供的脚本将通过msiexec
卸载Chrome,并根据提供的{PRODUCT_CODE}
在注册表中搜索。
假设它是msi安装并且远程powershell是启用的:
invoke-command -computername comp001 { uninstall-package 'google chrome' }
对于程序提供者(所有用户),它类似于:
get-package *chrome* | % { $_.metadata['uninstallstring'] }
"C:Program FilesGoogleChromeApplication95.0.4638.54Installersetup.exe" --uninstall --channel=stable --system-level --verbose-logging
然后运行uninstallstring,但是你必须找出静默卸载选项(——force-uninstall)。它也在后台运行。