Windows 所有广告用户的帐户到期日或帐户永不过期
当我运行下面的命令时,getWindows 所有广告用户的帐户到期日或帐户永不过期,windows,powershell,active-directory,Windows,Powershell,Active Directory,当我运行下面的命令时,getexpiration\u date为空 是否可以在expiration\u date中获取“永不过期”而不是空白 Import-Module ActiveDirectory $Groups = Get-ADGroup -filter {Name -like "SSL_VPN_Users" } | Select-Object Name ForEach ($Group in $Groups) { Get-ADGroupMember -identity $($group
expiration\u date
为空
是否可以在expiration\u date
中获取“永不过期”而不是空白
Import-Module ActiveDirectory
$Groups = Get-ADGroup -filter {Name -like "SSL_VPN_Users" } | Select-Object Name
ForEach ($Group in $Groups) {
Get-ADGroupMember -identity $($group.name) -recursive |
Get-ADUser -Properties samaccountname,mail,AccountExpires |
select samaccountname,mail,@{l="expiration_date";e={[datetime]::fromfiletime($_.accountexpires)}} |
Export-csv -path C:\SSLVPN\SSL_VPN_Users.csv -NoTypeInformation
}
问题可能是当帐户从不过期时,
AccountExpires
的值是最大的int64
值,这导致在调用[datetime]:FromFileTime
时出现ArgumentOutOfRangeException
异常
因此,请尝试以下操作-我引入了helper函数accountexpireestostring
,以提高表达式脚本块的可读性,但如果您愿意,可以将函数的代码直接打包到脚本块中
function accountExpiresToString($accountExpires) {
if (($_.AccountExpires -eq 0) -or
($_.AccountExpires -eq [int64]::MaxValue)) {
"Never expires"
}
else {
[datetime]::fromfiletime($accountExpires)
}
}
Import-Module ActiveDirectory
...
ForEach ($Group in $Groups) {
Get-ADGroupMember ... |
Get-ADUser -Properties ...,AccountExpires |
Select-Object @{l="expiration_date";e={ accountExpiresToString($_.AccountExpires)}} |
Export-Csv ...
}
更新:如果感兴趣,这里有一个描述0和0x7FFFFFFFFFFFFFFFFF(
[int64]::MaxValue
)表示永不过期的帐户 Get ADUser-Properties AccountExpirationDate
,而不是AccountExpires
就像你说的,我已经更改了它,但是没有luckThanks过期日期是“永不过期”的,但是如果我运行我的代码,对于一些用户来说,我仍然有“空白”而不是“永不过期”,它是“01.01.1601 02:00:00”而不是“空白”对于一些用户来说,这是什么问题?似乎0和[int64]::MaxValue都用来表示帐户永不过期-请参阅我的更新尝试运行时,我遇到了错误,就像您必须在“-”运算符的右侧提供值表达式一样。在第2行char:27表达式或语句中意外的标记“in”。我猜您使用的是较旧版本的PowerShell(我猜是2.0)-我更新了答案,将-in
运算符替换为-或。请再试一次,但我看不到一些用户的帐户到期日期,它将作为空白字段出现。如果我运行我的代码,我可以看到帐户到期日期,因为不设置一些用户的永不过期