如果程序存在卸载

问题描述 投票:0回答:1

我想了解.uninstall()方法。

this link看起来.uninstall()方法只有在与Get-WmiObject -Class Win32_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 char:3 + $ x86_check.uninstall()+ ~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo :InvalidOperation:(:) [],RuntimeException + FullyQualifiedErrorId:InvokeMethodOnNull

方法调用失败,因为[Microsoft.PowerShell.Commands.Internal。 Format.FormatStartData]不包含名为“Uninstall”的方法。在C:\ Users \ Admin \ Desktop \ test.ps1:18 char:3 + $ x64_check.uninstall()+ ~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo :InvalidOperation:(:) [],RuntimeException + FullyQualifiedErrorId:MethodNotFound

我认为根本原因是变量中有DisplayNameUninstallString,对吧?

我发现的一个出路是使用:

'"C:\Program Files\erl8.3\Uninstall.exe'" | cmd

为了卸载,但这不是使用我想要使用的.uninstall()方法。

微软是说你只能在32位架构上使用.uninstall()而对于64位你需要找到自己的出路吗?

如果是这样的话,这是非常简陋的

powershell methods uninstall
1个回答
0
投票

答复是否定的。

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

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

Get-Package "*Erlang*"

至少找到程序但是

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

不会卸载

© www.soinside.com 2019 - 2024. All rights reserved.