powershell - 为什么我的 PowerShell 脚本不遵守步骤顺序?
问题描述
我试图自己找到解决方案,但现在我没有想法。我写了一个脚本,我想用它来检查机器上是否安装了 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
解决方案
只需在最后添加 Format-Table 到 $x86_check 和 $64_check 即可。回答链接:https ://social.technet.microsoft.com/Forums/en-US/5f88f7c9-fbce-4c45-a2fa-806d512a0233/powershell-output-wrong-order?forum=ITCG
# 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
}
}
# Erlang check
Write-Host "Checking if Erlang exist " -NoNewline
Check_Program_Installed("Erlang")
Write-Host "The End: the script ends here" -ForegroundColor Yellow
推荐阅读
- linux - MySQL 工作台迁移向导错误“'int' 和 'NoneType 的实例之间不支持”
- python - pandas:根据列从另一个数据帧中查找每列的日志除以标量
- styled-components - styled-components 和 styled-components/macro 有什么区别
- sql - 我们可以通过 qldb 中的事务哈希检索文档修订吗
- python - 如何从 Selenium 中的变量构造 XPath?
- python - 在 Pandas 中按行/列合并两个数据框
- android - AccessibilityManager 未启用但通过 If 语句
- c# - 在 C# 中的另一个 MDI 表单中包含一个 MDI 表单
- ceph - 未及时深度清理的 Ceph PG 不断增加
- .net - 工作完成后自动停止控制台服务