首页 > 解决方案 > 使用PowerShell脚本卸载谷歌浏览器

问题描述

我正在使用以下代码从我的远程机器上卸载谷歌浏览器软件,但是这个脚本执行时没有输出。当我检查控制面板时,谷歌浏览器程序仍然存在。有人可以检查此代码吗?

foreach($computer in (Get-Content \path\to\the\file))
{
$temp1 = Get-WmiObject -Class Win32_Product -ComputerName $computer | where { $_.name -eq "Google Chrome"}   
$temp1.Uninstall()
}

标签: windowspowershellpowershell-remoting

解决方案


您不应该使用Win32_ProductWMI 类,枚举操作的副作用之一是它会检查每个已安装程序的完整性,并在完整性检查失败时执行修复安装。


相反,查询注册表以获取此信息更安全,该信息也恰好包含用于删除产品的卸载字符串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:\SOFTWARE\Wow6432node\Microsoft\Windows\CurrentVersion\Uninstall",
"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"

# 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:\Users\username\AppData\Local\Google\Chrome\Application\79.0.3945.130\Installer\setup.exe" --uninstall --channel=stable --verbose-logging --force-uninstall

今后,不建议在企业环境中使用ChromeSetup.exeChromeStandaloneSetup64.exe安装和管理 Chrome,您应该使用Enterprise MSI并在系统范围内安装,以便更有效地管理 Chrome。这是在企业环境中部署 Chrome 的受支持方式,我提供的脚本将用于卸载 Chrome,msiexec并在注册表中搜索{PRODUCT_CODE}提供的内容。


推荐阅读