42 lines
1.6 KiB
PowerShell
42 lines
1.6 KiB
PowerShell
|
function Get-ComputerUptime {
|
||
|
<#
|
||
|
.SYNOPSIS
|
||
|
Returns an OrderedDictionary that Represents the Computer Up Time.
|
||
|
#>
|
||
|
|
||
|
[CmdletBinding()]
|
||
|
Param()
|
||
|
|
||
|
$logLead = (Get-LogLeadName);
|
||
|
$providerStopWatch = [System.Diagnostics.StopWatch]::StartNew()
|
||
|
|
||
|
$uptimeDictionary = New-Object System.Collections.Specialized.OrderedDictionary
|
||
|
$uptimeDetails = New-Object System.Collections.Specialized.OrderedDictionary
|
||
|
|
||
|
try {
|
||
|
|
||
|
Write-Verbose "$logLead : [$($providerStopWatch.Elapsed)] : Getting Computer Uptime"
|
||
|
$uptimeSeconds = Get-CIMInstance -Namespace "root\CIMV2" -Class Win32_PerfFormattedData_PerfOS_System -Property SystemUpTime | Select-Object -ExpandProperty SystemUpTime
|
||
|
Write-Verbose "$logLead : [$($providerStopWatch.Elapsed)] : Computer Uptime Retrieved"
|
||
|
|
||
|
$uptimeTimeSpan = New-TimeSpan -Seconds $uptimeSeconds
|
||
|
$bootTime = (Get-Date).AddSeconds(-1 * $uptimeSeconds)
|
||
|
|
||
|
$uptimeDetails["UptimeString"] = ("{0:00}d {1:00}h {2:00}m {3:00}s" -f $uptimeTimeSpan.Days,
|
||
|
$uptimeTimeSpan.Hours, $uptimeTimeSpan.Minutes, $uptimeTimeSpan.Seconds);
|
||
|
$uptimeDetails["UptimeSeconds"] = $uptimeSeconds;
|
||
|
$uptimeDetails["LastBootTime"] = ("{0} {1}" -f $bootTime.ToShortDateString(), $bootTime.ToLongTimeString());
|
||
|
$uptimeDetails["LastBootTimeTicks"] = $bootTime.Ticks
|
||
|
}
|
||
|
catch {
|
||
|
|
||
|
$uptimeDetails["Error"] = $_.Exception.ToString()
|
||
|
}
|
||
|
|
||
|
$uptimeDictionary.Add("Uptime", $uptimeDetails)
|
||
|
|
||
|
Write-Verbose "$logLead : [$($providerStopWatch.Elapsed)] : Provider Complete"
|
||
|
$providerStopWatch.Stop()
|
||
|
|
||
|
return $uptimeDictionary
|
||
|
}
|