Question : Powershell script to update the computer description with the machine last contacted days and no of Hrs online.

Hi,

Powershell script to update the computer description with the machine last contacted days and no of Hrs online.
Script to update the details as
 
(Contacts  : 8  days) (Online : 12 hrs)

Regards
sharath

Answer : Powershell script to update the computer description with the machine last contacted days and no of Hrs online.


Updated.

Chris
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
$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)"
}
Random Solutions  
 
programming4us programming4us