Home » PowerShell » Calculating Server Uptime, Start Shutdown Crash

Calculating Server Uptime, Start Shutdown Crash


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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: