Calculating Server Uptime

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

Posted Jun 25 2010, 08:00 AM by ps1
Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.