如果程序存在卸载
If program exists uninstall
我正在尝试理解 .uninstall()
方法。
从 this link 看来,方法 .uninstall()
仅在与 Get-WmiObject -Class Win32_Product
一起使用时才有效。但这意味着它将只考虑 32 位软件而不考虑 64 位软件。
所以我写了这几行来卸载 64 位的 Erlang:
# 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
但不幸的是 returns 我的错误是:
You cannot call a method on a null-valued expression. At
C:\Users\Admin\Desktop\test.ps1:17 char:3
+ $x86_check.uninstall()
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Method invocation failed because
[Microsoft.PowerShell.Commands.Internal. Format.FormatStartData] does
not contain a method named 'Uninstall'. At
C:\Users\Admin\Desktop\test.ps1:18 char:3
+ $x64_check.uninstall()
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
我认为根本原因是变量中有DisplayName
和UninstallString
,对吗?
我发现的一个出路是使用:
'"C:\Program Files\erl8.3\Uninstall.exe'" | cmd
为了卸载,但这没有使用我想要使用的 .uninstall()
方法。
Microsoft 是说您只能在 32 位体系结构上使用 .uninstall()
,而对于 64 位体系结构,您需要找到自己的出路吗?
如果是这样,那还很简陋
我正在尝试理解 .uninstall()
方法。
从 this link 看来,方法 .uninstall()
仅在与 Get-WmiObject -Class Win32_Product
一起使用时才有效。但这意味着它将只考虑 32 位软件而不考虑 64 位软件。
所以我写了这几行来卸载 64 位的 Erlang:
# 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
但不幸的是 returns 我的错误是:
You cannot call a method on a null-valued expression. At C:\Users\Admin\Desktop\test.ps1:17 char:3 + $x86_check.uninstall() + ~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
Method invocation failed because [Microsoft.PowerShell.Commands.Internal. Format.FormatStartData] does not contain a method named 'Uninstall'. At C:\Users\Admin\Desktop\test.ps1:18 char:3 + $x64_check.uninstall() + ~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound
我认为根本原因是变量中有DisplayName
和UninstallString
,对吗?
我发现的一个出路是使用:
'"C:\Program Files\erl8.3\Uninstall.exe'" | cmd
为了卸载,但这没有使用我想要使用的 .uninstall()
方法。
Microsoft 是说您只能在 32 位体系结构上使用 .uninstall()
,而对于 64 位体系结构,您需要找到自己的出路吗?
如果是这样,那还很简陋