powershell - 使用 Search-ADAccount 查找所有未设置帐户到期日期的帐户

标签 powershell

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/

相关文章:

powershell - 将错误输出重定向到脚本内的文件

powershell - 使用 PowerShell 导入和导出 CSV,所有列都用双引号引起来

powershell - 如何在返回名称与驱动器相关的文件时查询临时 PS 驱动器?

arrays - 不同长度的数组到一个 CSV

powershell - 在多个父文件夹中使用 PowerShell 创建子文件夹

regex - 使用Powershell在文件中将路径从斜杠更改为反斜杠

powershell - 如何使用 PowerShell 查找 CPU 和 RAM 使用情况?

powershell - 如何使用 ssh 和 PowerShell 中的私钥连接到 linux 服务器?

powershell - 如何使用 jenkins 声明性管道凭证并转换为 powershell 凭证?

PowerShell 将文本行添加到多个文件