应find不在特定OU中的禁用用户的PowerShell脚本也会从该OU中输出用户

嘿,这里有这个cmdlet:

Get-ADUser -filter {(distinguishedName -notlike "Disabled Users") -and (enabled -eq $false)} -searchBase "ou=FirstOU,dc=domain,dc=com" 

我已经构build它来查找不在“禁用用户”OU中的禁用用户。 (OU内的OU)

但由于某种原因,它不仅会返回不在“禁用用户”中的禁用用户,还会返回其中的禁用用户。

为什么不(distinguishedName -notlike "Disabled Users")工作?

为了使我的结构清晰:

 Forest FirstOU users,groups,etc.. Disabled Users OU . . . 

括号和通配符。 尝试

 PS C:\Users\BigHomie> Get-ADUser -SearchBase "OU=Users,dc=eng,dc=mit,dc=edu" -SearchScope Subtree -Filter {distinguishedname -notlike "*Disabled*"} 

正确的语法在这里find

筛选器将处理您尝试检索的对象的types,在此情况下为User对象。 因此,您的查询返回dn不是“禁用用户”的任何禁用用户。 它将filter应用于User对象,而不是OU。

是的,当然…用户dn将包含BigHomie正确指出的string“Disabled Users”。 真正的问题是缺less通配符,因为用户DN不会完全“禁用用户”

试试这个:

 Get-ADUser -Filter {(Enabled -eq $false)} | ? { ($_.distinguishedname -notlike '*Disabled Users*') } 

您的查询不起作用,因为DN属性不支持LDAP查询中的通配符匹配 (和-notlike / -notlike没有通配符是无用的)。

您只需要检索所有禁用的用户,然后从结果中筛选不需要的帐户:

 $Disabled = Get-ADUser -Filter { useraccountcontrol -bor 2 } -SearchBase "ou=FirstOU,dc=domain,dc=com" $Filtered = $AllDisabledUsers |Where-Object {$_.distinguishedName -notmatch "OU=Disabled Users"} 

{ useraccountcontrol -bor 2 }相当于禁用帐户的“纯”LDAPfilter:
(&(useraccountcontrol:1.2.840.113556.1.4.803:=2))