Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .SYNOPSIS
- This script calculates the computer Uptime Of The Day.
- .DESCRIPTION
- This script shows how long the machine has been running today.
- It should works when machine exits sleep mode the first time of the day (ID 6013).
- .PARAMETER Verbose
- Used to specify weither or not display the list of the boots and reboots/shutdowns.
- .EXAMPLE
- .\UOTD.ps1 -Verbose
- #>
- param
- (
- [switch]$Verbose
- )
- $BootsArray = [System.Collections.ArrayList]@()
- $RestartsShutdownsArray = [System.Collections.ArrayList]@()
- Foreach($log in Get-EventLog -LogName system -After ([datetime]::Today) | ? { 6013 -contains $_.EventID} | sort TimeGenerated)#6013#6005
- {
- if(($log.TimeGenerated.Minute -ne 0) -and ($log.TimeGenerated.Second -ne 0))
- {
- $BootsArrayID = $BootsArray.Add($log.TimeGenerated)
- }
- }
- Foreach($logB in Get-EventLog -LogName system -After ([datetime]::Today) | ? { 6006 -contains $_.EventID} | sort TimeGenerated)
- {
- $RestartsShutdownsArrayID = $RestartsShutdownsArray.Add($logB.TimeGenerated)
- }
- if($Verbose)
- {
- Write-Output "Boots : "
- $BootsArray
- Write-Output "`nReboots/Shutdowns :`n "
- $RestartsShutdownsArray
- }
- $TotalSessionTime = New-TimeSpan
- For($i=0;$i -lt $BootsArray.Count;$i++)
- {
- if($i -lt $RestartsShutdownsArray.Count)
- {
- $SessionTime = New-TimeSpan –Start $BootsArray[$i] –End $RestartsShutdownsArray[$i]
- $TotalSessionTime += $SessionTime
- }
- else
- {
- $SessionTime = NEW-TIMESPAN –Start $BootsArray[$i] –End $(Get-Date)
- $TotalSessionTime += $SessionTime
- }
- }
- Write-Output "`nToday the machine has been running for a total of : $($TotalSessionTime.Hours)h $($TotalSessionTime.Minutes)m $($TotalSessionTime.Seconds)s"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement