我正在寻找一个 powershell 脚本,该脚本将返回根目录中的所有用户,并在帐户处于非活动状态超过 15 天和/或 30 天时对其进行标记。我想我已经很接近了,但我的 If/Elseif 语句似乎不起作用。有谁知道改善这一点的方法?
$objSearcher.Filter = "(&(objectCategory=person)(objectClass=user))"
$objSearcher.PropertiesToLoad.Add("displayName")
$objSearcher.PropertiesToLoad.Add("sAMAccountName")
$objSearcher.PropertiesToLoad.Add("lastLogonTimeStamp")
$userObjects = $objSearcher.FindAll()
foreach ($user in $userObjects)
{
$dn = $user.Properties.Item("displayName")
$sam = $user.Properties.Item("sAMAccountName")
$logon = $user.Properties.Item("lastLogonTimeStamp")
if($logon.Count -eq 0)
{
$lastLogon = "Never"
}
#Inactive for 15 days
elseif($logon.Value -ge (get-date).AddDays(-15))
{
$lastLogon = "Pending"
}
#Inactive for 30 days
elseif($logon.Value -ge (get-date).AddDays(-30))
{
$lastLogon = "Retired"
}
else
{
$lastLogon = [DateTime]$logon[0]
$lastLogon = $lastLogon.AddYears(1600)
}
"""$dn"",$sam,$lastLogon"
}
谢谢