使用 Powershell 的卸载程序出现语法错误

Syntax error with Uninstaller using Powershell

我正在尝试让我的通用卸载程序正常工作。这是我的代码:

CLS

$Software = "Zoom"
$Filter = "*" + $Software + "*"
$Program = $ProgUninstall = $FileUninstaller = $FileArg = $NULL

try 
{
    if (Test-Path -Path "HKLM:\SOFTWARE\WOW6432Node") 
    {
        $programs = Get-ItemProperty -Path "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" -ErrorAction Stop
    }

    $programs += Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*" -ErrorAction Stop
    $programs += Get-ItemProperty -Path "Registry::\HKEY_USERS\*\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*" -ErrorAction SilentlyContinue
} 
catch 
{
    Write-Error $_
    break
}

foreach($Program in $Programs)
{
    $ProgDisplayName = $Program.DisplayName
    $ProgUninstall = $Program.UninstallString

    if($ProgDisplayName -like $Filter)
    {
        #$Program

        $aux = $ProgUninstall -split @('\.exe'),2,[System.StringSplitOptions]::None
        $Uninstaller = (cmd /c echo $($aux[0].TrimStart('"').TrimStart("'") + '.exe')).Trim('"')
        $UninsParams = $aux[1].TrimStart('"').TrimStart("'").Trim().split(' ',[System.StringSplitOptions]::RemoveEmptyEntries)
        if($aux -notlike "param 0 = *")
        {
           # $UninsParams = $aux[1].TrimStart('"').TrimStart("'").Trim().split(' ',[System.StringSplitOptions]::RemoveEmptyEntries)
        }

        $Uninstaller
        $UninsParams

       # . $Uninstaller $UninsParams | Where-Object { $_ -notlike "param 0 = *" }
    }
}

在我的示例中,我尝试获取 Zoom 的输出。我安装了Zoom客户端和Zoom Outlook插件。

程序的输出如下:

DisplayName  : Zoom Outlook Plugin
PSPath       : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{0B76DE11-5937-4491-A66A-617E42170AFF}
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall
PSChildName  : {0B76DE11-5937-4491-A66A-617E42170AFF}
PSDrive      : HKLM
PSProvider   : Microsoft.PowerShell.Core\Registry

You cannot call a method on a null-valued expression.
At line:34 char:9
+         $UninsParams = $aux[1].TrimStart('"').TrimStart("'").Trim().s ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 

AuthorizedCDFPrefix : 
Comments            : Zoom
Contact             : Zoom Video Communications, Inc.
DisplayVersion      : 5.4.58891
HelpLink            : https://support.zoom.us/home
HelpTelephone       : 
InstallDate         : 20201119
InstallLocation     : 
InstallSource       : C:\temp\Zoom\
ModifyPath          : MsiExec.exe /X{3109C49B-F5E4-4FEC-8F6F-EC5E4626B361}
NoModify            : 1
Publisher           : Zoom
Readme              : 
Size                : 
EstimatedSize       : 122109
UninstallString     : MsiExec.exe /X{3109C49B-F5E4-4FEC-8F6F-EC5E4626B361}
URLInfoAbout        : https://zoom.us
URLUpdateInfo       : 
VersionMajor        : 5
VersionMinor        : 4
WindowsInstaller    : 1
Version             : 84207115
Language            : 1033
DisplayName         : Zoom
PSPath              : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{3109C49B-F5E4-4FEC-8F6F-EC5E4626B36
                      1}
PSParentPath        : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall
PSChildName         : {3109C49B-F5E4-4FEC-8F6F-EC5E4626B361}
PSDrive             : HKLM
PSProvider          : Microsoft.PowerShell.Core\Registry

据我所知,部分问题是 Zoom Outlook 插件没有任何与之关联的卸载字符串。我假设它只是 Zoom Client 的一部分(即使它是一个单独的安装程序)。我想要做的是让这段代码正常工作,而不会抛出任何错误或显示误报。

这是我的 2 个参数“$Uninstaller”和“$UninsParams”的输出

You cannot call a method on a null-valued expression.
At line:34 char:9
+         $UninsParams = $aux[1].TrimStart('"').TrimStart("'").Trim().s ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
.exe
/X{3109C49B-F5E4-4FEC-8F6F-EC5E4626B361}
MsiExec.exe
/X{3109C49B-F5E4-4FEC-8F6F-EC5E4626B361}

提前致谢!

这里的问题是,由于 Zoom 客户端插件没有卸载脚本,您的 -split 操作评估为 一个空字符串 ,并且 $aux[1] 因此求值为 $null,因此出现错误消息。

您可以过滤掉没有有效 UninstallString 的条目:

foreach($Program in $Programs |Where-Object UninstallString -match '\.exe')
{
    # ... now you don't need to worry about this not resulting in two strings
    $aux = $ProgUninstall -split @('\.exe'),2,[System.StringSplitOptions]::None
}