简体   繁体   中英

Syntax error with Uninstaller using Powershell

I'm trying to get my universal uninstaller working. Here is my code:

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 = *" }
    }
}

In my example I'm trying to get an output for Zoom. I have the Zoom client installed and the Zoom Outlook plugin installed.

Here is the output of the program:

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

Part of the problem from what I can tell is the Zoom Outlook plugin doesn't have any uninstall strings associated with it. I'm assuming it's just part of the Zoom Client (even though it's a seperate installer). What I'm trying to do is get this code to work without throwing any errors or displaying false positives.

Here is the output of my 2 parms "$Uninstaller" and "$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}

Thanks in advance!

The problem here is that since the Zoom Client Plugin doesn't have an uninstall script, your -split operation evaluates to a single empty string , and $aux[1] therefore evaluates to $null , hence the error message.

You could filter out entries without a valid 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM