问题描述
我目前正在开发一个 Powershell GUI 脚本,以帮助我的团队更轻松地找到密码过期、禁用帐户等的帐户,并将其输出到 CSV。它几乎完全围绕“Get-ADUser”命令展开。到目前为止,几乎一切都奏效了,除非找到密码过期的帐户。
我已经对此进行了大量研究,但似乎没有简单使用 Get-ADUser 查找过期帐户的方法。我知道我可以使用 Search-ADAccount 代替,但这样做会很尴尬(因为我需要重新编写大量代码)。
Get-Aduser -Properties * -Filter {PasswordExpired -eq $true}
只是画一个空白。
例如
Get-ADUser -Properties * -Filter * | ? {$_.PasswordExpired -eq $True -and $_.Enabled -eq $true} | Select-Object name,enabled | Export-Csv "C:\report.csv" -NoTypeinformation
工作得很好,但如果我尝试分配命令的“中间”,即
{$_.PasswordExpired -eq $True -and $_.Enabled -eq $true}
到一个变量,然后将其替换到命令中,我要么得到一个错误,一个在我的 AD 中的所有这些的列表,要么根本没有。替换变量的理由是考虑可能的帐户状态(用户可以通过选择单选按钮进行选择)。
我尝试了双引号和单引号的各种排列,包括但不包括大括号等,但 Powershell 不会让我休息!
谢谢!
解决方法
Get-ADUser
cmdlet 公开 PasswordExpired
扩展属性,它是一个布尔值,指示密码是否已过期。它基于 msDS-User-Account-Control-Computed
属性。但是,您无法使用此属性进行过滤。
这意味着您可以检查该属性的 UF_PASSWORD_EXPIRED 位:
Get-ADUser -Filter "Enabled -eq 'True'" -Properties 'msDS-User-Account-Control-Computed' |
Where-Object {($_.'msDS-User-Account-Control-Computed' -band 0x800000) -eq 0x800000} | # UF_PASSWORD_EXPIRED --> 0x800000 = 8388608
Select-Object Name,Enabled | Export-Csv "C:\report.csv" -NoTypeInformation
您可以通过扩展过滤器以排除 PasswordNeverExpires
和 PasswordNotRequired
都为 $false 的用户来加快上述速度:
$filter = "Enabled -eq 'True' -and PasswordNeverExpires -eq 'False' -and PasswordNotRequired -eq 'False'"
Get-ADUser -Filter $filter -Properties PasswordNeverExpires,PasswordNotRequired,'msDS-User-Account-Control-Computed' |
Where-Object {($_.'msDS-User-Account-Control-Computed' -band 0x800000) -eq 0x800000} | # UF_PASSWORD_EXPIRED --> 0x800000 = 8388608
Select-Object Name,Enabled | Export-Csv "C:\report.csv" -NoTypeInformation
,
我想我已经在 Stack Exchange 上找到了解决方案。
早期测试表明它有效。