在 powershell 中获取已启用且未锁定的 Active Directory 用户列表

Get a list of enabled and not locked-out Active Directory Users in powershell

我有以下 cmdlet:

  get-aduser -filter {Enabled -eq 'true' -and LockedOut -eq 'true'} -Properties samaccountname, givenname, sn, physicalDeliveryOfficeName | 
             Select-Object -Property samaccountname, givenname, sn, physicalDeliveryOfficeName |
             Export-Csv -Encoding Default -path $csvOutputPath -NoTypeInformation

现在我需要在上面的 get-aduser cmdlet 中添加一个过滤器来排除锁定的 AD 用户....但我不知道该怎么做....

我知道我可以使用以下方法获得锁定的用户:

搜索-ADAccount -UsersOnly -锁定

有什么想法吗?

如果您想将其保留在一个查询中,您可以针对 UserAccountControl 属性 进行过滤。文档显示锁定的帐户标记了 0x0010(十六进制)或 16(十进制基数 10)位。因此,您应该能够使用按位与运算符 (-band) 来确定锁定状态。

get-aduser -filter {Enabled -eq 'true' -and (-not (UserAccountControl -band 16))} -Properties samaccountname, givenname, sn, physicalDeliveryOfficeName | 
             Select-Object -Property samaccountname, givenname, sn, physicalDeliveryOfficeName |
             Export-Csv -Encoding Default -path $csvOutputPath -NoTypeInformation

-not 运算符用于未锁定的帐户。如果您想查找锁定的帐户,可以将其删除。

从技术上讲,您可以对整个过滤器使用 UserAccountControl,但它不是人类可读的。下面是 -band 运算符在这里如何工作的示例:

# Enabled User not locked has useraccountcontrol = 512
[bool](512 -band 16)
False

# Enabled User locked has useraccountcontrol = 528
PS C:\temp\test1> [bool](528 -band 16)
True

# Locked Enabled User with expired password
[bool](8389136 -band 16)
True

有关用于确定 UserAccountControl 值的位的详细信息,请参阅 UserAccountControl Flags

有关按位运算符的详细信息,请参阅 About_Arithmetic_Operators