【问题标题】:combine multiple conditions in if-statement在 if 语句中组合多个条件
【发布时间】:2018-10-12 13:03:50
【问题描述】:

当您希望 ONE 集合或 OTHER 集合的 2 个条件为真时,如何将 4 个条件链接在一起?

更准确地说,我想做:

如果用户已登录且操作系统版本为 Windows 10

用户已登录且 LogonUI 进程未运行

不要理会这些命令,它们在隔离时都可以正常工作,我的问题是将它们链接在一起。

例如我有:

if (
        (Get-WmiObject –ComputerName $poste –Class Win32_ComputerSystem).UserName`
        -and`
        (Get-WmiObject -Computer $poste -Class Win32_OperatingSystem).Version -like "*10*"
    )
    { echo do X }

这工作正常。我想在同一个if 中添加另一个条件块。我试过了,但它不起作用:

if (
        (Get-WmiObject –ComputerName $poste –Class Win32_ComputerSystem).UserName`
        -and`
        (Get-WmiObject -Computer $poste -Class Win32_OperatingSystem).Version -like "*10*"`
        -or`
        (Get-WmiObject –ComputerName $poste –Class Win32_ComputerSystem).UserName`
        -and -not`
        (Get-Process -ErrorAction SilentlyContinue -ComputerName $poste -name logonui)
    )
    { echo do X }

你如何像这样链接多个“块”?我知道我可以做两个不同的 IF,我让它工作但没有办法在一个 IF 中将它们全部链接在一起吗? (IF包含很多代码,我不想用两个IF复制它)

【问题讨论】:

    标签: powershell if-statement boolean-logic


    【解决方案1】:

    将每组条件放在括号中:

    if ( (A -and B) -or (C -and D) ) {
        echo do X
    }
    

    如果任一第一个第二组条件必须为真(但不能同时为真)使用-xor而不是-or

    if ( (A -and B) -xor (C -and D) ) {
        echo do X
    }
    

    将 A、B、C 和 D 替换为相应的表达式。

    【讨论】:

    • 谢谢,这有助于缩短代码并且更有意义。代码放在一起看起来有点混乱,但它工作正常。
    【解决方案2】:

    如果您想让您自己的答案中的代码更易于理解,您可以删除重复的代码以使if 语句更清晰。

    将结果分配给变量并使用这些变量:

    $UserName = Get-WmiObject –ComputerName $poste –Class Win32_ComputerSystem | select -ExpandProperty UserName
    $WindowsVersion = Get-WmiObject -Computer $poste -Class Win32_OperatingSystem | select -ExpandProperty Version
    $LogonuiProcess = Get-Process -name logonui -ComputerName $poste -ErrorAction SilentlyContinue
    

    然后:

    if (($UserName -and $WindowsVersion -like "*10*") -or ($UserName -and -not $LogonuiProcess)) {Write-Output "do X"}
    

    或者

    if ($UserName -and $WindowsVersion -like "*10*") {Write-Output "do X"}
    elseif ($UserName -and -not $LogonuiProcess) {Write-Output "do Y"}
    

    【讨论】:

      【解决方案3】:

      所以在尝试了几件事之后,似乎有两种方法:

      if (
              (Get-WmiObject –ComputerName $poste –Class Win32_ComputerSystem).UserName`
              -and`
              (Get-WmiObject -Computer $poste -Class Win32_OperatingSystem).Version -like "*10*"`
      
          ) { echo do X }
      
          elseif (
      
              (Get-WmiObject –ComputerName $poste –Class Win32_ComputerSystem).UserName`
              -and -not`
              (Get-Process -ErrorAction SilentlyContinue -ComputerName $poste -name logonui)
      
          )   { echo do X }
      

      或使用 Ansgar Wiechers 的出色答案将其全部链接到一个 IF:

      if (
              (      
              (Get-WmiObject –ComputerName $poste –Class Win32_ComputerSystem).UserName`
              -and`
              (Get-WmiObject -Computer $poste -Class Win32_OperatingSystem).Version -like "*10*"`
              ) -or`
              (       
              (Get-WmiObject –ComputerName $poste –Class Win32_ComputerSystem).UserName`
              -and -not`
              (Get-Process -ErrorAction SilentlyContinue -ComputerName $poste -name logonui)`
              )
      
          ) { echo do X }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-12-17
        • 2015-01-20
        • 1970-01-01
        • 1970-01-01
        • 2017-09-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多