Powershell 中有没有办法列出域中未设置帐户到期日期的所有 AD 帐户?即帐户到期日期设置为“从不”。
我可以利用 Get-ADuser 来获取单个帐户的到期时间。
get-aduser username | Select-Object -Property name,Enabled,AccountExpirationDate,DistinguishedName | Where-Object {$_.AccountExpirationDate -eq '`n'}
但是,有没有办法使用“Search-ADAccount -AccountExpiring.....”来获取域中没有设置帐户到期日期的所有帐户的列表,或者有更好的方法来执行此操作?
当我使用以下命令时,我没有收到任何返回信息,但我设置了一个没有设置到期日期(即从不)的测试帐户来测试该命令,但该帐户不会显示在结果中。
Search-ADAccount -AccountExpiring | Select-Object -Property name,AccountExpirationDate | Where-Object {$_.AccountExpirationDate -eq '`n'}
最佳答案
使用 Get-ADUser 的解决方案
如果您使用正确的过滤器,您仍然可以使用 Get-ADUser
查找帐户:
Get-ADUser -Filter {-not(AccountExpirationDate -like "*") } -Properties AccountExpirationDate | Select-Object -Property name,Enabled,AccountExpirationDate,DistinguishedName
这将为您提供未设置属性 AccountExpirationDate
值的每个帐户。
使用搜索 ADAccount 的解决方案
如果您仍想使用 Search-ADAccount
,您必须提供一个时间戳,直到帐户过期 (take a look at the ref) 。没有选项可以搜索未设置任何值的帐户。您总是必须提供某种日期时间。
例如,如果您想查找 2 周后过期的所有帐户,您必须选择:
第一:
带有帐户到期日期的解决方案:
Search-ADAccount -AccountExpiring -DateTime ((Get-Date).AddDays(14))
第二:
帐户到期前剩余天数的解决方案:
Search-ADAccount -AccountExpiring -Timespan "14"
关于powershell - 使用 Search-ADAccount 查找所有未设置帐户到期日期的帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55015804/