簡體   English   中英

使用 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操作評估為單個空 string ,因此$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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM