为什么我的 PowerShell 脚本不遵守步骤顺序? [复制]

2023-12-24

我试图自己找到解决方案,但现在我没有主意了。我编写了一个脚本,我想用它来检查机器上是否安装了 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 末尾即可。Link 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么我的 PowerShell 脚本不遵守步骤顺序? [复制] 的相关文章

  • 使用 PEM 证书验证 Powershell 中的 XML 签名

    我正在尝试创建一个 powershell 脚本来使用 XML 文档中的数据 然而 在进行任何工作之前 我需要通过验证签名来验证 XML 没有被篡改 我有一份用于以 PEM 格式签署 XML 的证书的公钥副本 但我不知道如何让 powersh
  • Powershell从字符串中获取数字

    大家好 我正在尝试使用 powershell 从用户 ID 中获取号码 我们使用的格式是名字的第一个字母 姓氏的前四个字母和学生 ID 因此名为 John Smith ID 123456 的学生将是 jsmit123456 如果用户的名字少
  • Docker 在 Powershell 中登录 gcr.io

    我正在尝试使用 Windows 10 登录 Google 容器注册表JSON 密钥文件 https cloud google com container registry docs advanced authentication using
  • 更改每个命令的 powershell 标题

    我想在窗口标题中显示我在 powershell 中输入的最后一个命令 以便更容易找到 目前我有 C gt host ui rawui WindowTitle 但这只是获取相对于我输入时的上一个命令 所以如果我有 C gt cd C gt h
  • 在 PowerShell 中提取 EXIF 数据的简单方法?

    我一直在研究使用 PowerShell 提取 EXIF 数据的各种方法 但到目前为止我发现它相当复杂 一些here http blog cincura net 233463 renaming files based on exif data
  • 如何在 Visual Studio 中调试 Windows PowerShell 模块?

    我正在尝试使用 Visual Studio 2015 将 PowerShell 模块编写为 VB NET 项目 我已经能够将一些命令放在一起 将类库编译到 DLL 中 将模块导入到 PowerShell 会话中并调用我创建的命令 到目前为止
  • 输入屏蔽密码

    我从其他帖子中拼凑了一个简单的例子 CmdletBinding DefaultParameterSetName Secret Param Parameter Mandatory True string FileLocation Parame
  • PowerShell,如何在现有浏览器选项卡中打开 URL?

    我想制作一个 powershell 脚本 它将 运行 Chrome 打开 google com 网站 停留一段时间 在同一选项卡中从 google com gt bing com 转到 这就是适合我的代码 它运行 Chrome 并在选项卡中
  • 如何使用 cypress 登录 Salesforce?

    我目前正在使用 cypress 进行一些测试 但是 我必须与 salesforce 进行一些测试 似乎我遇到了以下问题 哎呀 没有可以运行的测试 context Salesforce gt beforeEach gt cy request
  • 如何使用 echo 命令写入并附加到文件

    我正在尝试编写一个脚本 它将使用 echo 并写入 附加到文件 但我的语法中已经有了 字符串 说 echo I am Finding difficult to write this to file gt file txt echo I ca
  • 将unix时间转换为字符串的Powershell函数?

    我正在尝试使用 ADO NET 和 PowerShell 读取 Firefox place sqlite datedase 的日期字段 显然这些字段是 Unix 时间的 我正在寻找 Net 日期时间或字符串的转换 Edit 我想将数据行绑定
  • 命令提示符 con 的 Powershell 通讯员?

    例如 我想在屏幕上显示输出并将其复制到剪贴板 dir tee con clip 上面的方法不起作用 因为con在 PowerShell 的文件系统中不被识别为控制台 还可能存在以下场景 Get LongLongOutput tee con
  • R 脚本自动化时的不同结果

    以下命令对 pdf 文件执行 Ghostscript 这pdf file变量包含该 pdf 的路径 bbox lt system paste C gs gs8 64 bin gswin32c exe sDEVICE bbox dNOPAUS
  • 如何通过在原始文件名前添加序列号来重命名文件?

    伙计们 有谁知道我该怎么做 我试图通过在文件名的开头添加 1 2 3 等按数字顺序列出一些文件 同时保留文件的原始名称 这是我尝试过的代码 nr 1 Dir path C x y deneme Rename Item NewName 0 N
  • 获取对 SOAP 的 XML 响应中的节点值

    我在 PowerShell 中发出 SOAP 请求 如下所示 uri https secure echosign com services EchoSignDocumentService20 WSDL sun Invoke WebReque
  • Powershell 4 对于 tls 1.2 来说太旧了?

    我有一个带有 powershell 4 的 Windows Server 2012R2 一个sql作业发出一个Invoke WebRequest https someserver file xml OutFile c tmp data xm
  • Word 2010 自动化:“转到书签”

    我有一个用 Delphi 7 编写的程序 它打开一个基于模板的新 Word 文档 文档打开后 系统会自动跳转到书签 在模板中预定义 并在其中添加一些文本 以下代码在 Word 2003 中工作正常 但会导致invalid variant o
  • 启动 psexec 后获取进程 ID

    我有一个使用 psexec 在远程计算机上调用记事本的脚本 有什么办法可以在启动后获取进程ID吗 这是我所拥有的 PCname MyPC SessionID 2 Program Notepad exe FilePath C temp Fil
  • Powershell 脚本无法从 SCCM 正常运行 [已关闭]

    这个问题不太可能对任何未来的访客有帮助 它只与一个较小的地理区域 一个特定的时间点或一个非常狭窄的情况相关 通常不适用于全世界的互联网受众 为了帮助使这个问题更广泛地适用 访问帮助中心 help reopen questions 我正在尝试
  • 如何在RobotFramework中进行多行测试设置或拆卸而不创建新关键字?

    我需要在测试用例中调用两个拆卸关键字 但不能为此创建新关键字 我很有趣 如果有这样的关键字语法 例如文档或循环 Documentation line1 line2 line3 使用 运行关键词 http robotframework org

随机推荐