如何在 Powershell 中为变量分配多个 'where' 条件

How to assign multiple 'where' conditions to variable in Powershell

我正在开发一个 Powershell 脚本(带有 GUI)来帮助我的同事更轻松地找到冗余和禁用的 AD 帐户。

这是一个小预览...

$props = "Name, Enabled, PasswordExpired, Company,passwordNeverExpires, Office"

$propsAsArray = $props -split ',\s*'

Get-ADUser -filter * -properties $propsAsArray | where {$_.Enabled -eq $true} | where {$_.PasswordNeverExpires -eq $false}| where {$_.passwordexpired -eq $false} | Select-Object $propsAsArray | Export-csv -Path "C:\report.csv"

一切正常并输出 CSV 报告。

问题在于如何将 AD 帐户状态的所有可能组合和排列分配给一个变量,然后将变量代入 Get-ADUser cmdlet(取决于用户在图形用户界面)。

我已经尝试了所有能想到的,但只能找回错误 Expressions are only allowed as the first element of a pipeline

我确定 $accountStatus = "where {$_.Enabled -eq $true} | where {$_.PasswordNeverExpires -eq $false}"(或微妙的变体)不是它的完成方式。

我对 Powershell 比较陌生,很想获得经验。谢谢,威廉。

您可以通过将每个条件与 :

串联来压缩多个 Where-Object 调用
Get-ADUser -Filter * -Properties $propsAsArray | Where-Object {(($_.Enabled -eq $true) -and ($_.PasswordNeverExpires -eq $false)) -and ($_.passwordexpired -eq $false)} | Select-Object $propsAsArray | Export-csv -Path "C:\report.csv"

但是正如 Olaf 已经在评论中指出的那样,最好已经使用 Get-ADUser-Filter 参数。在那里,您可以使用类似的条件组合:

Get-ADUser -Filter {((Enabled -eq $true) -and (PasswordNeverExpires -eq $true)) -and (passwordexpired -eq $false)} -Properties $propsAsArray | Select-Object $propsAsArray | Export-csv -Path "C:\report.csv"

注意:此答案解决了 所问的问题 ,使用 广义 Where-Object-based solution based on script blocks ({ ... }), but in the case at hand a string-based solution based on Get-ADUser's -Filter parameter, which efficiently filters at the source, as shown in the second command in 是更可取的。


存储一个 script blocks ({ ... }) 的数组,表示变量中的条件,并使用索引数组 select 根据用户的情况应用哪些条件GUI select离子:

# All individual conditions to test, expressed as an array of script blocks.
# Note: No need for `-eq $true` to test Boolean properties, and
#       `-eq $false` is better expressed via the `-not` operator.
$blocks = 
  { $_.Enabled },
  { -not $_.PasswordNeverExpires },
  { $_.PasswordExpired }


# Select the subset of conditions to apply using AND logic, using 
# an array of indices, based on the GUI selections.
$indices = 0..2   # e.g., all 3 conditions (same as: 0, 1, 2)

Get-ADUser -Filter * -properties $propsAsArray | Where-Object { 
  # The following is equivalent to combining the conditionals of interest
  # with -and (AND logic):
  foreach ($block in $blocks[$indices]) { # Loop over selected conditionals
    if (-not (& $block)) { return $false } # Test, and return $false instantly if it fails.
  }
  $true # Getting here means that all conditions were met.
}

注意每个块是如何通过 &call operator.

执行的