通过使用PowerShell,可以计算出各个域控制器的最新值,并在其基础上计算出“最终”的LastLogon值。以下是示例代码:
# Get the AD search base.
$ADSearchBase = "DC=your,DC=domain,DC=com"
# Get all enabled AD users.
$AllADUsers = Get-ADUser -Filter * -Property *
# Get the lastLogon property for each user from all domain controllers.
$LastLogonArray = @{}
foreach ($DC in (Get-ADDomainController -Filter *)) {
$LastLogonArray += (Get-ADUser -Filter * -Server $DC.HostName -Properties lastLogon)
}
# Calculate the latest lastLogon value for each user.
$MostRecentLastLogon = @{}
foreach ($ADUser in $AllADUsers) {
$DN = $ADUser.DistinguishedName
$MostRecentLastLogon[$DN] = ($LastLogonArray | ?{$_.DistinguishedName -eq $DN} |
Measure-Object -Property lastLogon -Max).Max
}
# Compare the lastLogon value for each user with the value in MMC AD Add-ins.
foreach ($ADUser in $AllADUsers) {
$DN = $ADUser.DistinguishedName
$LastLogonMMCAD = $ADUser.LastLogonTimeStamp
$LastLogonPowerShell = $MostRecentLastLogon[$DN]
if ($LastLogonMMCAD -ne $LastLogonPowerShell) {
Write-Host "$($ADUser.SamAccountName)'s LastLogon value in MMC AD Add-ins ($($LastLogonMMCAD)) does not match the value calculated by PowerShell ($($LastLogonPowerShell))."
}
}