搜索属性为空的用户

Search user with empty attributes

我尝试搜索具有空属性的所有用户,例如 telephoneNumber(多属性查询)。 在网上我找到了很多关于PowerShell和LDAP的资料,但是很复杂,没有一个很好用。

LDAP 过滤器的语法很丑陋,但一旦您理解了它们就没那么难了 their structure。基本上,每个子句都放在一组括号中:

(attribute=value)

子句可以被否定:

(!(attribute=value))

并且可以通过逻辑 AND 或 OR 运算组合多个子句:

(&(attribute=somevalue)(otherattribute=othervalue)...)
(|(attribute=somevalue)(otherattribute=othervalue)...)

要过滤空属性,您需要一个 LDAP 过滤器说 "attribute telephoneNumber does not have any value":

(!(telephoneNumber=*))

星号 (*) 是任意非空值的通配符。否定该条款会给你你想要的。对于更复杂的过滤器,您可以像上面那样制作子句(每个属性一个),并根据您的要求将它们与 (&...) and/or (|...) 组合。

示例(获取 telephoneNumbermail 属性为空的用户):

Get-ADUser -LDAPFilter '(|(!(telephoneNumber=*))(!(mail=*)))'

另一种选择是获取所有用户并使用 Where-Object 过滤器来选择具有所需属性的用户。

示例(同样,获取 telephoneNumbermail 属性为空的用户):

Get-ADUser -Filter * -Properties * | Where-Object {
  -not $_.telephoneNumber -or
  -not $_.mail
}