Skip to content

Instantly share code, notes, and snippets.

@MyITGuy
Last active October 8, 2021 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MyITGuy/7871e15a289e23060ba6ab0de07cf5b3 to your computer and use it in GitHub Desktop.
Save MyITGuy/7871e15a289e23060ba6ab0de07cf5b3 to your computer and use it in GitHub Desktop.
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