Saturday, March 2, 2019

A simple check for power outages

Sometimes users leave their computers on for longer intervals than usual only to see later that they have stopped or worse - dead because there were power surges. Reasons for that - one of them is the power outage. Many haven't a good protection like UPS or backup power sources and it's good to know exactly when this happened. Relatively it helps to estimate when to shutdown the hardware preventing it from losing data or become broken. One simple way is to check when this occured with the following PowerShell command (assuming the user is using Windows Vista and above):

Get-EventLog -LogName System -EntryType Error | Where-Object {$_.EventID -eq 6008} | Select -Property Message | ForEach-Object { if ($_.Message -match "([0-9]{1,2}:.*[0-9]{4})") { Write-Output "System shutdown at $($Matches[1])"}}

This parses whole Event Log on the machine with level Error in System Log where the event ID is unexpected system shutdown (eventID 6008) and outputs the results using regexp for extracting the date and time:

System shutdown at 2:05:00 PM on ‎3/‎1/‎2019
System shutdown at 1:32:30 PM on ‎2/‎27/‎2019
System shutdown at 9:28:38 AM on ‎1/‎25/‎2019
System shutdown at 2:45:59 PM on ‎1/‎21/‎2019
System shutdown at 4:15:13 PM on ‎1/‎10/‎2019
System shutdown at 9:57:27 AM on ‎1/‎10/‎2019
System shutdown at 3:16:15 PM on ‎12/‎27/‎2018
System shutdown at 6:33:47 PM on ‎12/‎23/‎2018
System shutdown at 6:29:35 PM on ‎12/‎23/‎2018
System shutdown at 6:26:43 PM on ‎12/‎23/‎2018
System shutdown at 5:53:58 PM on ‎12/‎23/‎2018
System shutdown at 5:34:58 PM on ‎12/‎23/‎2018
System shutdown at 10:40:03 AM on ‎12/‎23/‎2018
System shutdown at 10:19:27 AM on ‎11/‎24/‎2018
System shutdown at 3:47:47 PM on ‎11/‎17/‎2018
System shutdown at 3:07:32 PM on ‎10/‎14/‎2018