Powershell 查找 7 天后过期的用户

标签 powershell active-directory pattern-matching user-accounts

我正在尝试运行一个 powershell 脚本来查询在 7 天内过期的帐户,我目前有

$a = (get-date).AddDays(7) ; Search-ADAccount -AccountExpiring -TimeSpan "7" | Select-Object SamAccountName,AccountExpirationDate | Sort-Object AccountExpirationDate | Export-Csv 7_days.csv



但是,当我进行以下更改时,似乎遇到了一些麻烦,最终得到了一个空的 CSV 文件。最终,我希望帐户在 7 天后到期,不多也不少。

$a = (get-date).AddDays(7) ; Search-ADAccount -AccountExpiring -TimeSpan "7" | Select-Object SamAccountName,AccountExpirationDate | Sort-Object AccountExpirationDate | Where-Object {$_.AccountExpirationDate -like $a } | Export-Csv 7_days.csv



有人可以让我知道我做错了什么吗?我曾尝试移动 "Where-Object {$_.AccountExpirationDate -like $a } "部分,或 "-match"而不是 "-like",但是这些并没有让我取得太大的成功。我哪里出错了?

最佳答案

更新:如果您传递一个字符串值,您可以获得帐户,传递一个整数会将时间跨度初始化为 7 个滴答声!

Search-ADAccount -AccountExpiring -TimeSpan "7"

其他有效选项:
Search-ADAccount -AccountExpiring -TimeSpan (New-TimeSpan -Days 7)
Search-ADAccount -AccountExpiring -TimeSpan ([TimeSpan]::FromDays(7))

可能是一个错误,它对我也不起作用。这是一个解决方法:
$NeverExpires = 9223372036854775807
$ExpringIn = (Get-Date).AddDays(7) 

Get-ADUser -Filter * -Properties accountExpires | 
Where-Object {$_.accountExpires -ne $NeverExpires  -and [datetime]::FromFileTime([int64]::Parse($_.accountExpires)) -lt $ExpringIn }

关于Powershell 查找 7 天后过期的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10247334/

相关文章:

powershell - 相当于 Powershell 中的 `type -a`

powershell - 需要 PowerShell 2.0 替换 string.replace 方法

c# - 如何验证 LDAP+SSL 连接的服务器 SSL 证书

c# - 获取主组 ID

haskell - "symmetric"函数的模式

Java 正则表达式字符串#replaceAll 替代

scala - 当给定这个模式与整数值匹配时,为什么 scala 会提示?

c# - 在 C# 中调用 Get-Smbshare

powershell - 如何从管理程序中以编程方式查找虚拟机的 IP 地址?

active-directory - Active Directory 跨域搜索