使用PowerShell脚本卸载谷歌浏览器

     2023-03-27     275

关键词:

【中文标题】使用PowerShell脚本卸载谷歌浏览器【英文标题】:uninstall google chrome using powershell script 【发布时间】:2021-12-08 11:50:11 【问题描述】:

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

foreach($computer in (Get-Content \path\to\the\file))

$temp1 = Get-WmiObject -Class Win32_Product -ComputerName $computer | where  $_.name -eq "Google Chrome"   
$temp1.Uninstall()

【问题讨论】:

【参考方案1】:

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


查询注册表以获取此信息更安全,该信息也恰好包含用于删除带有msiexec 的产品的卸载字符串。这里的卸载字符串将被格式化为MsiExec.exe /XPRODUCT_CODE_GUIDPRODUCT_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 /XPRODUCT_CODE_GUID

如果您还有其他不是由上述语法引起的问题,这将是一个操作问题,最好在https://superuser.com 站点进行故障排除。


编辑

通过我们的chat 对话发现,您正在按用户安装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 的受支持方式,我提供的脚本可以通过 msiexec 卸载 Chrome 并在注册表中搜索提供的 PRODUCT_CODE

【讨论】:

我没有如此广泛地使用 powershell,因为我需要在 500 多个虚拟机中卸载,所以我尝试了这个。我不明白最后一行。你能完成吗? 这里是Invoke-Command 文档的Examples 部分的链接。您可以将上面的代码放在一个脚本块中并使用Invoke-Command 运行它。第一个示例显示了如何连接到远程计算机并执行此操作。注意:我建议像这样分块执行大规模删除;不要尝试一次完成所有 500 个。 @George 忘记在卸载命令中添加 /qn 选项。代码示例已更新。此参数抑制提示。您还可以使用 /qn 自动安装新软件。 @George 你为什么不和我一起in chat,我会尽力帮助你解决这个问题? @George 我已经用新版本更新了上面的脚本,该版本将使用所使用的任一安装方法读取注册表以了解 chrome 的存在,并相应地卸载。这里的最新版本也会等待 EXE 命令完成,之前的版本没有。【参考方案2】:

假设它是一个 msi 安装并且启用了远程 powershell:

invoke-command -computername comp001  uninstall-package 'google chrome'  

对于程序提供者(所有用户),它类似于:

get-package *chrome* | %  $_.metadata['uninstallstring'] 

"C:\Program Files\Google\Chrome\Application\95.0.4638.54\Installer\setup.exe" --uninstall --channel=stable --system-level --verbose-logging

然后运行该卸载字符串,但您必须找出静默卸载选项(--force-uninstall)。它也在后台运行。

【讨论】:

OP 和我在聊天中确定他们使用的是非 msi 版本,所以这种方法行不通。另外,Uninstall-Package 可以与 MSI 安装的软件一起使用吗?

PowerShell 5.1 - 如何卸载当前使用的模块

】PowerShell5.1-如何卸载当前使用的模块【英文标题】:PowerShell5.1-Howtouninstallmodulewhichiscurrentlyuse【发布时间】:2017-07-2608:56:45【问题描述】:我们在一个部署PowerShell脚本中使用了一些PowerShell模块。使用以下命令,我们将模块(即... 查看详情

Powershell:通过 UpgradeCode 卸载应用程序

】Powershell:通过UpgradeCode卸载应用程序【英文标题】:Powershell:UninstallapplicationbyUpgradeCode【发布时间】:2018-08-1100:07:00【问题描述】:当我通过Powershell脚本升级/降级我的应用程序时,我想先强制卸载当前安装的版本,然后再运... 查看详情

尝试使用 selenium python 脚本登录谷歌

...并填写了电子邮件回复,尽管在提交时我收到了错误“此浏览器或应用程序可能不安全。了解详情尝试使用不同的浏览器。如果您已经在使用受支持的浏览器,则可以 查看详情

Powershell:卸载提示用户输入的软件

】Powershell:卸载提示用户输入的软件【英文标题】:Powershell:UninstallSoftwarethathaspromptsforuserinput【发布时间】:2016-02-1213:34:24【问题描述】:如果提示用户输入,是否有人知道是否可以通过PowerShell卸载软件?我有多个脚本可以卸... 查看详情

PowerShell - HTML 解析:从网站获取信息

】PowerShell-HTML解析:从网站获取信息【英文标题】:PowerShell-HTMLparsing:getinformationfromawebsite【发布时间】:2012-02-2115:20:30【问题描述】:更新,脚本正在使用PowerShellV3.0,谢谢@Doug我想使用以下PowerShell脚本从Lufthansa获取航班状态信... 查看详情

检测并卸载防病毒软件

...】:2018-10-2321:10:17【问题描述】:我一直在尝试制作一个powershell脚本来检测安装了什么杀毒软件,然后卸载它。我已经能够检测到使用WMI安装了哪些防病毒软件。但是我找不到通过powershell卸载杀毒软件的方法。有没有办法做到... 查看详情

怎样卸载googlechrome浏览器

googlechrome浏览器卸载方法如下(以windowsxp为例):1、打开控制面板,点击添加删除程序2、在添加删除页面中找到Googlechrome浏览器,点击删除。参考技术A回答如何卸载谷歌浏览器:1、首先在“开始”中打开“控制面板”,并在... 查看详情

使用powershell卸载exe软件

】使用powershell卸载exe软件【英文标题】:Uninstallaexesoftwareusingpowershell【发布时间】:2019-01-1900:57:38【问题描述】:我正在关注thisthread从我的Windows10系统中卸载mozillafirefox。我最初使用exe安装程序安装了mozillafirefox,但没有得到执... 查看详情

用于检查浏览器中启用的 TLS 1.2 的 Powershell 脚本

】用于检查浏览器中启用的TLS1.2的Powershell脚本【英文标题】:PowershellscripttocheckTLS1.2enabledinbrowser【发布时间】:2021-11-2222:24:26【问题描述】:我可以有一个脚本来检查注册表中以下位置启用的tls1.2。•HKEY_LOCAL_MACHINE\\SYSTEM\\Current... 查看详情

将谷歌浏览器卸载后,重新安装,书签没有了怎么找回来?

将谷歌浏览器卸载后,重新安装,书签没有了怎么找回来?可以使用一个搜索软件比如everything来查找,输入Bookmarks进行查找,在找到的几个结果中可能有类似于C:\Users\Administrator\AppData\Local\Google\Chrome\UserData&#... 查看详情

PowerShell 添加任务以使用参数运行 PowerShell 脚本

】PowerShell添加任务以使用参数运行PowerShell脚本【英文标题】:PowerShelladdTasktorunPowerShellscriptwithparameters【发布时间】:2013-05-1909:41:42【问题描述】:我正在尝试从PowerShell脚本将任务添加到任务计划程序,该脚本将运行带有参数... 查看详情

使用Javascript检测谷歌浏览器切换CSS

】使用Javascript检测谷歌浏览器切换CSS【英文标题】:UsingJavascripttodetectGoogleChrometoswitchCSS【发布时间】:2012-05-0207:54:28【问题描述】:我一直在使用不同的脚本,我发现一个适用于除Chrome之外的所有脚本...这是我在.CSS文件中用来... 查看详情

loadrunner使用火狐浏览器录制脚本问题

问题一:刚安装好的loadrunner使用firefox录制脚本,firefox浏览器不自动弹出、或打开一次后第二次打不开。  解决方法:  火狐浏览器之前有卸载过没有卸载干净,会导致打开一次又再打开找不到对应的路径,在c盘... 查看详情

从 powershell 卸载产品

】从powershell卸载产品【英文标题】:Uninstallaproductfrompowershell【发布时间】:2016-04-1706:14:09【问题描述】:如何使用产品的guid卸载产品我试过了msiexec/xguid但我无法卸载该产品我用的时候效果很好Wmicproductwhereidentifyingnumber=guidcallun... 查看详情

powershell使用powershell下载文件的脚本。(代码片段)

查看详情

使用 Powershell 脚本生成 Azure Databricks 令牌

】使用Powershell脚本生成AzureDatabricks令牌【英文标题】:GenerateAzureDatabricksTokenusingPowershellscript【发布时间】:2019-06-0423:26:04【问题描述】:我需要。我已经完成了使用ARM模板创建AzureDatabricks的工作,现在我正在寻找使用powershell脚... 查看详情

使用其他语言激活 Powershell 脚本

】使用其他语言激活Powershell脚本【英文标题】:Powershellscriptactivationusingotherlanguages【发布时间】:2021-10-0408:23:45【问题描述】:我有一个PS脚本,我需要在用户无需打开powershell的情况下运行它——我认为最简单的事情是编写一... 查看详情

Powershell 脚本 - 如何一次删除多个包?

】Powershell脚本-如何一次删除多个包?【英文标题】:PowershellScript-HowdoIremovemultiplepackagesatonce?【发布时间】:2020-07-2700:10:18【问题描述】:我正在尝试用更好的代码扩展我现有的脚本。所以我想让脚本证明软件是否真的被卸载了... 查看详情