【发布时间】:2019-06-18 01:39:40
【问题描述】:
我试图自己找到解决方案,但现在我没有想法。我写了一个脚本,我想用它来检查机器上是否安装了 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)
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)
}
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
}
}
# Erlang check
Write-Host "Checking if Erlang exist " -NoNewline
Check_Program_Installed("Erlang")
Write-Host "The End: the script ends here" -ForegroundColor Yellow
但如果我执行它作为结果,最后一行将在Check_Program_Installed("Erlang") 之前执行。为什么 PowerShell 不尊重步骤优先级?
Checking if Erlang exist On this computer is installed
The End: the script ends here
DisplayName UninstallString
----------- ---------------
Erlang OTP 21 (10.2) C:\Program Files\erl10.2\Uninstall.exe
【问题讨论】:
-
与您的问题无关,但您将 x86/x64 混淆了,这可能是进一步的潜在问题。
-
@notjustme 是对的:
$x86_check -and $x64_check -eq $null返回 true 如果程序安装为 32 位(而不是 64 位)但是函数返回(错误)“$programName 未安装在这台计算机上”。阅读Know your operator and enclosure precedence
标签: powershell scripting automation