为什么我的PowerShell脚本是不尊重的步骤顺序? [重复](Why my PowerShe

2019-09-27 13:01发布

这个问题已经在这里有一个答案:

  • 无法暂停或选择-对象入睡后 2个回答

我试图找到自己的解决方案,但现在我的想法。 我写了,我想用它来检查是否安装在计算机上的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

Answer 1:

只需添加格式表,以$ 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


Answer 2:

我曾尝试复制你的代码和测试它,但我不能让实际Check_Program_Installed功能的工作,所以我很难验证。

然而,PowerShell来理解管道是非常重要的。

通常与你所做的一切,它被发送到管道将出现在您的控制台中为了使管道得到它。

但是写主机不会将您的文本发送到管道,直接将其发送到控制台,因此它可能是一个简单的竞争条件,其中文本获取到控制台比管道对象更快。

如果您尝试使用测试Write-Output ,而不是Write-Host我猜你在正确的顺序,因为Write-Output通过管道发送对象到控制台。



文章来源: Why my PowerShell script is not respecting the steps order? [duplicate]