Windows

PS 適用於過去 90 天內無人登錄的 OU

  • January 3, 2020

我知道如何獲取 ADUser 的最後一次登錄,但我真正想知道的是哪些 OU 在過去 90 天內沒有使用者登錄。AD 在數百個公司 OU 中有數千名使用者,我想知道哪些 OU 處於非活動狀態。謝謝你。


$Date = Get-Date
$Date90 = $Date.AddDays(-90)
$OUs = Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchBase 'OU=XX Users,DC=XXX,DC=XXX'
# Check each OU.
ForEach ($OU In $OUs)
{
   $Base = $($OU.DistinguishedName)
   # Query for all users directly in the OU that have logged on in the last specified number of days.
   # Do not consider any child OUs.
   $ActiveUsers = Get-ADUser -SearchBase $Base -SearchScope OneLevel -Filter {LastLogonDate -ge $Date90}
   If ($ActiveUsers.Count -eq 0)
   {
       # Make sure the OU has at least one user.
       $TotalUsers = Get-ADUser -SearchBase $Base -SearchScope OneLevel -Filter *
       If ($TotalUsers.Count -gt 0) {"OU $Base has no users that have logged on in the last 90 days"}
   }
}

引用自:https://serverfault.com/questions/997289