Get System Downtime

Determine your Powershell exectuion policy

1
Get-ExecutionPolicy

Change your Powershell exectuion policy if restricted

1
Set-ExecutionPolicy RemoteSigned

Script to get system downtime

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
37
38
[timespan]$downTime = New-TimeSpan -start 0 -end 0
[timespan]$totalDownTime = New-TimeSpan -start 0 -end 0
Get-EventLog -LogName system |
Where-Object `
{ $_.eventid -eq 6005 -OR $_.eventID -eq 6006 -AND $_.timegenerated -gt (get-date).adddays(-30) } |
Sort-Object -Property timegenerated |
Foreach-Object `
{
  if ($_.EventID -eq 6006)
     {
       $down = $_.TimeGenerated
     } #end if eventID
  Else
     {
      $up = $_.TimeGenerated
     } #end else
   if($down -AND $up)
     {
      if($down -ge $up)
         {
           Write-Host -foregroundColor Red "*** Invalid data. Ignoring $($up)"
           $up = $down
          } #end if down is greater than up
       [timespan]$CurrentDownTime = new-TimeSpan -start $down -end $up
       [timeSpan]$TotalDownTime = $currentDownTime + $TotalDownTime
       $down = $up = $null
     } #end if down and up
} #end foreach

"Total down time on $env:computername is:"
$TotaldownTime

$minutesInMonth = (24*60)*30
$minutesInDay = 24*60
$downTimeMinutes = $TotaldownTime.TotalMinutes
$percentUpTime = "{0:n2}" -f (100 - ($downTimeMinutes/$minutesInMonth)*100)

"This computes to $percentUptime percent uptime on $env:computerName for the current 30 day period"
//