$DomainControllers = "dc1", "dc2", "dc3", "dc4"
$Computers = @{}
$DomainControllers | ForEach-Object {
Get-QADComputer -SearchRoot "domain.com/Offices" -Service $_ -SizeLimit 0 -IncludedProperties LastLogon | `
Select-Object Name, DN, LastLogon | ForEach-Object {
If ($Computers.$($_.DN)) {
$Computers.$($_.DN) = $_ | Select-Object Name, DN, `
@{n='LastLogon';e={
If ($_.LastLogon -gt $Computers.$($_.DN).LastLogon) {
$_.LastLogon
} Else {
$Computers.$($_.DN).LastLogon
} }}
} Else {
$Computers.Add($_.DN, $_)
}
}
}
$Computers.Values | ForEach-Object {
$LastLogon = (New-TimeSpan $_.LastLogon).Days
If (Test-Connection $_.Name -Quiet -Count 1) {
$LastBootUpTime = (Get-WmiObject Win32_OperatingSystem -Computer $_.Name).LastBootUpTime
If ($LastBootUpTime -ne $Null) {
$LastBootUpTime = [Management.ManagementDateTimeConverter]::ToDateTime($LastBootUpTime)
$Online = [Math]::Round((New-TimeSpan $LastBootUpTime).TotalHours, 2)
}
} Else {
$Online = 0
}
Set-QADComputer $_.DN -Description "(Contacts: $LastLogon days) (Online: $Online hours)"
}
|