通过 PowerShell 卸载应用程序

Uninstall Applications Via PowerShell

我已经为此工作了几天了,无论我如何 运行 和工作,它似乎都是通过 PowerShell 和 returns 卸载程序的成功代码:

__GENUS          : 2
__CLASS          : __PARAMETERS
__SUPERCLASS     :
__DYNASTY        : __PARAMETERS
__RELPATH        :
__PROPERTY_COUNT : 1
__DERIVATION     : {}
__SERVER         :
__NAMESPACE      :
__PATH           :
ReturnValue      : 0
PSComputerName   :

这种情况发生在各种众所周知的难以删除的软件上,例如 McAfee。

正在使用的命令是:

 Get-WmiObject -Class win32_product -Filter "Name like '%McAfee%'" | ForEach-Object {$_.Uninstall()}

我在这里尝试了各种脚本、解决方案以及它们的变体(如下所示)。

$uninstall32 = gci "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_ -match "Yahoo Messenger" } | select UninstallString

$uninstall64 = gci "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_ -match "Yahoo Messenger" } | select UninstallString

if ($uninstall64) {
    $uninstall64 = $uninstall64.UninstallString -Replace "msiexec.exe","" -Replace "/I","" -Replace "/X",""
    $uninstall64 = $uninstall64.Trim()
    Write "Uninstalling (x64)..."
    start-process "msiexec.exe" -arg "/X $uninstall64 /qb" -Wait
    }
if ($uninstall32) {
    $uninstall32 = $uninstall32.UninstallString -Replace "msiexec.exe","" -Replace "/I","" -Replace "/X",""
    $uninstall32 = $uninstall32.Trim()
    Write "Uninstalling (x32)..."
    start-process "msiexec.exe" -arg "/X $uninstall32 /qb" -Wait}

即使像 Yahoo Messenger 这样简单的东西,当 运行 从 Powershell Window 作为管理员 returns 成功代码 and/or 时,命令也无法卸载应用程序不再出现在 WMI 应用程序列表中。

您可以检查 MSIInstaller 事件以找到卸载失败原因的线索:

Get-WinEvent -computername <computername> -ProviderName MSIInstaller -Maxevents 30

您还可以记录 MSI activity 并将 /le '<logfilepath>' 添加到您对 msiexec.exe 的调用并检查结果。

我相信 msi install/uninstall 操作是异步的。您可能需要在 pssession 中等待,直到安装完成。

McAfee Agent 有时需要删除 frminst.exe /forceuninsall