Advertisement
Combreal

UOTD.ps1

Nov 24th, 2020 (edited)
943
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .SYNOPSIS
  3. This script calculates the computer Uptime Of The Day.
  4.  
  5. .DESCRIPTION
  6. This script shows how long the machine has been running today.
  7. It should works when machine exits sleep mode the first time of the day (ID 6013).
  8.  
  9. .PARAMETER Verbose
  10. Used to specify weither or not display the list of the boots and reboots/shutdowns.
  11.  
  12. .EXAMPLE
  13. .\UOTD.ps1 -Verbose
  14. #>
  15.  
  16. param
  17. (
  18.     [switch]$Verbose
  19. )
  20.  
  21. $BootsArray = [System.Collections.ArrayList]@()
  22. $RestartsShutdownsArray = [System.Collections.ArrayList]@()
  23.  
  24. Foreach($log in Get-EventLog -LogName system  -After ([datetime]::Today) | ? { 6013 -contains $_.EventID} | sort TimeGenerated)#6013#6005
  25. {
  26.     if(($log.TimeGenerated.Minute -ne 0) -and ($log.TimeGenerated.Second -ne 0))
  27.     {
  28.         $BootsArrayID = $BootsArray.Add($log.TimeGenerated)
  29.     }
  30. }
  31.  
  32. Foreach($logB in Get-EventLog -LogName system  -After ([datetime]::Today) | ? { 6006 -contains $_.EventID} | sort TimeGenerated)
  33. {
  34.     $RestartsShutdownsArrayID = $RestartsShutdownsArray.Add($logB.TimeGenerated)
  35. }
  36.  
  37. if($Verbose)
  38. {
  39.     Write-Output "Boots : "
  40.     $BootsArray
  41.     Write-Output "`nReboots/Shutdowns :`n "
  42.     $RestartsShutdownsArray
  43. }
  44.  
  45. $TotalSessionTime = New-TimeSpan
  46. For($i=0;$i -lt $BootsArray.Count;$i++)
  47. {
  48.     if($i -lt $RestartsShutdownsArray.Count)
  49.     {
  50.         $SessionTime = New-TimeSpan –Start $BootsArray[$i] –End $RestartsShutdownsArray[$i]
  51.         $TotalSessionTime += $SessionTime
  52.     }
  53.     else
  54.     {
  55.         $SessionTime = NEW-TIMESPAN –Start $BootsArray[$i] –End $(Get-Date)
  56.         $TotalSessionTime += $SessionTime
  57.     }
  58. }
  59.  
  60. Write-Output "`nToday the machine has been running for a total of : $($TotalSessionTime.Hours)h $($TotalSessionTime.Minutes)m $($TotalSessionTime.Seconds)s"
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement