Calculating Server Uptime

by Jun 25, 2010

Have you ever wanted to find out the effective uptime of your PC (or some servers)? The information can be found inside the event log. Here is an example on how to select and prepare event data and create a report. This even works remotely.

function Get-UpTime {
param($computername=‘localhost’)

Get-WmiObject win32_NTLogEvent Filter ‘Logfile=”System” and EventCode>6004 and EventCode<6009’ -ComputerName $computername |
ForEach-Object {
$rv = $_ | Select-Object EventCode, TimeGenerated
switch ($_.EventCode) {
6006 { $rv.EventCode = ‘shutdown’ }
6005 { $rv.EventCode = ‘start’ }
6008 { $rv.EventCode = ‘crash’ }
}
$rv.TimeGenerated = $_.ConvertToDateTime($_.TimeGenerated)
$rv
}
}

Get-Uptime