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



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

CLS
$Software = "Zoom"
$Filter = "*" + $Software + "*"
$Program = $ProgUninstall = $FileUninstaller = $FileArg = $NULL
try 
{
if (Test-Path -Path "HKLM:SOFTWAREWOW6432Node") 
{
$programs = Get-ItemProperty -Path "HKLM:SOFTWAREWOW6432NodeMicrosoftWindowsCurrentVersionUninstall*" -ErrorAction Stop
}
$programs += Get-ItemProperty -Path "HKLM:SOFTWAREMicrosoftWindowsCurrentVersionUninstall*" -ErrorAction Stop
$programs += Get-ItemProperty -Path "Registry::HKEY_USERS*SOFTWAREMicrosoftWindowsCurrentVersionUninstall*" -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.CoreRegistry::HKEY_LOCAL_MACHINESOFTWAREWOW6432NodeMicrosoftWindowsCurrentVersionUninstall{0B76DE11-5937-4491-A66A-617E42170AFF}
PSParentPath : Microsoft.PowerShell.CoreRegistry::HKEY_LOCAL_MACHINESOFTWAREWOW6432NodeMicrosoftWindowsCurrentVersionUninstall
PSChildName  : {0B76DE11-5937-4491-A66A-617E42170AFF}
PSDrive      : HKLM
PSProvider   : Microsoft.PowerShell.CoreRegistry
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:tempZoom
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.CoreRegistry::HKEY_LOCAL_MACHINESOFTWAREWOW6432NodeMicrosoftWindowsCurrentVersionUninstall{3109C49B-F5E4-4FEC-8F6F-EC5E4626B36
1}
PSParentPath        : Microsoft.PowerShell.CoreRegistry::HKEY_LOCAL_MACHINESOFTWAREWOW6432NodeMicrosoftWindowsCurrentVersionUninstall
PSChildName         : {3109C49B-F5E4-4FEC-8F6F-EC5E4626B361}
PSDrive             : HKLM
PSProvider          : Microsoft.PowerShell.CoreRegistry

据我所知,问题的一部分是Zoom Outlook插件没有任何卸载字符串。我认为它只是Zoom客户端的一部分(尽管它是一个单独的安装程序(。我想做的是让这个代码在不抛出任何错误或显示误报的情况下工作。

这是我的2个parms"的输出$卸载程序";以及"$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
}

最新更新