You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PowerShell脚本远程卸载Google Chrome失败,请求代码排查

问题排查

  • 空值无校验问题:如果Get-WmiObject没有匹配到符合条件的程序,$temp1会是$null,直接调用Uninstall()不会有任何输出也不会执行操作,可先加if($temp1)判断是否查到对象,再执行卸载。
  • Win32_Product类的天生缺陷:
    • 该类只能识别通过MSI安装包安装的程序,现在大部分Chrome安装包是EXE格式,不会写入Win32_Product的列表,自然查不到
    • 每次调用Win32_Product都会自动触发所有已安装MSI程序的修复校验,运行速度极慢,还可能意外修改其他程序的配置
  • 程序名匹配不准确:部分Chrome的安装记录显示名可能带版本后缀,全等匹配会查不到
  • 远程权限问题:远程调用WMI需要目标设备开放WMI入站规则,且执行脚本的账号有目标设备的本地管理员权限,否则查询会静默失败
  • 机器列表读取失败:你代码里Get-Content \path\to\the\file用的是根路径相对写法,若当前执行上下文的工作目录不对,会读取不到设备列表,相当于循环根本没有执行

修复方案

建议用更稳定的注册表查询方式找卸载路径,替换原有逻辑,示例代码如下:

foreach($computer in (Get-Content "C:\absolute\path\to\device\list.txt")) {
    # 先测试设备连通性
    if(-not (Test-Connection -ComputerName $computer -Count 1 -Quiet)) {
        Write-Host "$computer 不在线,跳过"
        continue
    }
    try {
        # 远程查询注册表的Chrome卸载项
        $uninstallReg = Invoke-Command -ComputerName $computer -ScriptBlock {
            $regPaths = @(
                "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*",
                "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*"
            )
            Get-ItemProperty $regPaths | Where-Object { $_.DisplayName -like "*Google Chrome*" } | Select-Object -First 1 UninstallString, QuietUninstallString
        } -ErrorAction Stop

        if($uninstallReg) {
            # 调用静默卸载参数
            $uninstallCmd = $uninstallReg.QuietUninstallString + " --force-uninstall"
            Invoke-Command -ComputerName $computer -ScriptBlock {
                cmd /c $using:uninstallCmd
            } -ErrorAction Stop
            Write-Host "$computer 卸载Chrome已执行"
        } else {
            Write-Host "$computer 未查到Chrome安装记录"
        }
    } catch {
        Write-Host "$computer 操作失败:$_"
    }
}

内容的提问来源于stack exchange,提问作者Raghav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 07:30:01