PowerShell'或'声明

Luk*_*e K 9 powershell

我正在尝试浏览Active Directory,并抓住符合特定条件的用户.我想让用户拥有Manager A或Manager B,但我不确定如何实现or语句.这是我的代码:

Get-ADUser -Filter * -Properties country, extensionattribute9 | if (extensionattribute9 -eq 'Smith, Joe') or (extensionattribute9 -eq 'Doe, John') {select extensionsattribute9, country}
Run Code Online (Sandbox Code Playgroud)

在此代码中,它无法识别extensionattribute9,它为您提供了用户的管理员.

我也试过尝试使用where而不是if,但无济于事.

Ans*_*ers 17

操作员-or不是or.见about_Logical_Operators.此外,if语句不从管道读取.将if语句置于ForEach-Object循环中:

... | ForEach-Object {
  if ($_.extensionattribute9 -eq 'Smith, Joe' -or $_.extensionattribute9 -eq 'Doe, John') {
    $_ | select extensionsattribute9, country
  }
}
Run Code Online (Sandbox Code Playgroud)

或者使用Where-Object声明:

... | Where-Object {
  $_.extensionattribute9 -eq 'Smith, Joe' -or
  $_.extensionattribute9 -eq 'Doe, John'
 } | Select-Object extensionsattribute9, country
Run Code Online (Sandbox Code Playgroud)

而且你不能自己使用属性名称.使用当前对象变量($_)来访问当前对象的属性.

要检查属性是否具有给定数量的值之一,您还可以使用-contains运算符而不是进行多重比较:

'Smith, Joe', 'Doe, John' -contains $_.extensionattribute9
Run Code Online (Sandbox Code Playgroud)