Last active
October 8, 2021 14:31
-
-
Save MyITGuy/7871e15a289e23060ba6ab0de07cf5b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-SleepHistory { | |
[CmdletBinding()] | |
PARAM( | |
[string[]] | |
$ComputerName = @($env:COMPUTERNAME) | |
) | |
begin { | |
} | |
process { | |
foreach ($Item In $ComputerName) { | |
try { | |
$GetEventLogSplat = @{ | |
LogName = 'System' | |
InstanceId = 1 | |
Source = 'Microsoft-Windows-Power-TroubleShooter' | |
ErrorAction = 'SilentlyContinue' | |
} | |
if (@('.', 'localhost', $env:COMPUTERNAME) -notcontains $Item) { | |
$GetEventLogSplat.ComputerName = $Item | |
} | |
# get hibernation events | |
Get-EventLog @GetEventLogSplat | ForEach-Object { | |
$Entry = $_ | |
# create new hash table for results | |
$Properties = [ordered]@{ | |
ComputerName = $Entry.MachineName | |
SleepTime = [System.DateTime]$Entry.ReplacementStrings[0] | |
WakeTime = [System.DateTime]$Entry.ReplacementStrings[1] | |
DurationHours = $null | |
WakeSource = [System.String]$Entry.ReplacementStrings[14] | |
} | |
$TimeSpan = New-TimeSpan -Start $Properties.SleepTime -End $Properties.WakeTime | |
$Properties.DurationHours = [System.Math]::Round($TimeSpan.TotalHours, 2) | |
# return object | |
[PSCustomObject]$Properties | |
} | |
} catch { | |
Throw $_ | |
} | |
} | |
} | |
end { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment