Powershell 如果程序存在,请卸载

Powershell 如果程序存在,请卸载,powershell,methods,uninstallation,Powershell,Methods,Uninstallation,我试图理解.uninstall()方法 从它看来,方法.uninstall()仅在与Get WmiObject-Class Win32\u Product一起使用时有效。但是这意味着它只考虑32位软件而不是64位软件。 所以我写了这几行来卸载Erlang,它是64位的: # Check if a Software ins installed function Check_Program_Installed($programName) { $x86_check = ((Get-ChildI

我试图理解
.uninstall()
方法

从它看来,方法
.uninstall()
仅在与
Get WmiObject-Class Win32\u Product
一起使用时有效。但是这意味着它只考虑32位软件而不是64位软件。 所以我写了这几行来卸载Erlang,它是64位的:

# Check if a Software ins installed
function Check_Program_Installed($programName) {
    $x86_check = ((Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall") |
                 Get-ItemProperty |
                 Where-Object {$_.DisplayName -like "*$programName*" } |
                 Select-Object -Property DisplayName, UninstallString) |
                 Format-Table

    if (Test-Path 'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall') {
        $x64_check = ((Get-ChildItem "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall") | Get-ItemProperty | Where-Object {$_.DisplayName -like "*$programName*" } | Select-Object -Property DisplayName, UninstallString) | Format-Table
    }
    if ($x86_check -and $x64_check -eq $null) {
        Write-Host "$programName is not installed on this computer" -ForegroundColor Green
        #continue
    } elseif ($x86_check -or $x64_check -ne $null) {
        Write-Host "On this computer is installed " -ForegroundColor Red
        $x86_check
        $x64_check

        $x86_check.uninstall()
        $x64_check.uninstall()
    }
}

# Erlang check
Write-Host "Checking if Erlang exist    " -NoNewline
Check_Program_Installed("Erlang")
Write-Host "The End: the script ends here" -ForegroundColor Yellow
但不幸的是,它返回了一个错误:

不能对空值表达式调用方法。在 C:\Users\Admin\Desktop\test.ps1:17字符:3 +$x86_check.uninstall() + ~~~~~~~~~~~~~~~~~~~~~~ +CategoryInfo:InvalidOperation:(:)[],运行时异常 +FullyQualifiedErrorId:InvokeMethodOnFull

方法调用失败,因为 [Microsoft.PowerShell.Commands.Internal.Format.FormatStartData]没有 不包含名为“卸载”的方法。在 C:\Users\Admin\Desktop\test.ps1:18字符:3 +$x64_check.uninstall() + ~~~~~~~~~~~~~~~~~~~~~~ +CategoryInfo:InvalidOperation:(:)[],运行时异常 +FullyQualifiedErrorId:MethodNotFound

我认为根本原因是变量中有
DisplayName
UninstallString
,对吗

我发现的一个解决方法是使用:

'"C:\Program Files\erl8.3\Uninstall.exe'" | cmd
为了卸载,但这不是使用我想要使用的
.uninstall()
方法

Microsoft是否说您只能在32位体系结构中使用
.uninstall()
,而对于64位,您需要找到自己的解决方法


如果是这样的话,这是非常基本的回答是否定的

.uninstall()
只能与
Get WmiObject-Class Win32_Product
一起使用,因此只能卸载32位程序

可能有另一种方法可以通过以下方式卸载32位和64位程序:

Get-Package "*Erlang*"
至少找到了程序,但是

Get-Package "*Erlang*" | Uninstall-Package -Force

不会卸载

回复为否。

.uninstall()
只能与
Get WmiObject-Class Win32_Product
一起使用,因此只能卸载32位程序

可能有另一种方法可以通过以下方式卸载32位和64位程序:

Get-Package "*Erlang*"
至少找到了程序,但是

Get-Package "*Erlang*" | Uninstall-Package -Force

不会卸载请勿使用
格式-*
cmdlet进行进一步的编程处理-这些cmdlet仅用于创建显示输出。该错误消息表示未安装
x86
版本。您需要为每个注册表项添加一个测试。您当前的测试仅针对“一个或另一个”。不要将
格式-*
cmdlet用于进一步的编程处理-这些cmdlet仅用于创建用于显示的输出。该错误消息表示未安装
x86
版本。您需要为每个注册表项添加一个测试。您当前的测试仅针对“一个或另一个”。