获取ADUser-搜索过期帐户.在命令中使用变量



我目前正在开发Powershell GUI脚本,以帮助我的团队更容易地找到密码过期的帐户、禁用的帐户等,并将其输出到CSV。它几乎完全围绕着">获取ADUser";命令到目前为止,除了查找密码过期的帐户外,几乎所有的方法都奏效了。

我已经对此进行了大量研究,但似乎没有使用Get-ADUser查找过期帐户的简单方法。我知道我可以使用搜索ADAccount,但这样做会很尴尬(因为我需要重写很多代码(。

Get-Aduser -Properties * -Filter {PasswordExpired -eq $true}只是一片空白。

我在找到了部分解决方案https://serverfault.com/questions/805526/get-aduser-password-expired-filter-not-working-correctly/805611

例如,

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-ADUsercmdlet公开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

您可以通过扩展过滤器来排除PasswordNeverExpiresPasswordNotRequired都为$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上找到了一个解决方案。

请参阅https://serverfault.com/questions/723217/find-out-if-password-expired-or-when-it-expires-for-user-in-a-specific-ou

早期测试表明它有效。

最新更新