如果自定义 userType 属性是 indexed,那么在这两种情况下您都可以利用 Active Directory Filter:
Get-ADUser -LDAPFilter "(&(!manager=*)(userType=Employee))" -Properties Department |
Select-Object Name, SamAccountName, Department
简要说明 LDAP Filter 正在做什么:
(& # AND, all conditions must be met
(!manager=*) # manager attribute is not populated
(userType=Employee) # usertype attribute is equal to "Employee"
) # close then AND clause
如果自定义属性未编入索引,则必须使用 PowerShell 完成过滤:
Get-ADUser -LDAPFilter "(!manager=*)" -Properties Department, userType |
Where-Object userType -EQ 'Employee' |
Select-Object Name, SamAccountName, Department
至于你的代码失败的原因,你在Select-Object语句之后缺少管道,当使用Where-Object过滤多个条件时,我们必须使用scriptblock。总之,以下会起作用(但比上面的例子慢很多)。
Get-ADUser -Filter * -Properties Department, userType, Manager |
Where-Object { $_.userType -eq 'Employee' -and -not $_.Manager } |
Select-Object Name, SamAccountName, Department