Created
May 27, 2016 15:41
-
-
Save paalbra/6802f85ddb6020ec70bce7b5005c9d0c to your computer and use it in GitHub Desktop.
This file contains hidden or 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-Uptime { | |
<# | |
.SYNOPSIS | |
Gets the uptime of computers. | |
.DESCRIPTION | |
Gets the uptime of computers. | |
.EXAMPLE | |
Get-Uptime -ComputerName "computer01","computer02" | Sort-Object -Descending "TotalSeconds" | Format-Table "PSComputerName","PrettyString" | |
#> | |
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory=$false)] [string[]] $ComputerName = $null | |
) | |
Begin { | |
Set-StrictMode -Version "Latest" | |
Set-Variable -Name "ErrorActionPreference" -Scope "Script" -Value "Stop" | |
[HashTable] $Arguments = @{} | |
# Done to prevent calling localhost if no computers is given. The calling user might have the right to call WMI, but not Invoke-Command on localhost. | |
if ($ComputerName -ne $null) { | |
$Arguments = @{"ComputerName" = $ComputerName} | |
} | |
} | |
Process { | |
Invoke-Command @Arguments -ScriptBlock { | |
$WMI = Get-WmiObject -Class Win32_OperatingSystem | |
$TimeSpan = $WMI.ConvertToDateTime($WMI.LocalDateTime) – $WMI.ConvertToDateTime($WMI.LastBootUpTime) | |
$PrettyString = "" | |
if ($TimeSpan.TotalDays -gt 0) { | |
$PrettyString += "$($TimeSpan.Days) days, " | |
} | |
if ($TimeSpan.TotalHours -gt 0) { | |
$PrettyString += "$($TimeSpan.Hours) hours, " | |
} | |
if ($TimeSpan.TotalMinutes -gt 0) { | |
$PrettyString += "$($TimeSpan.Minutes) minutes, " | |
} | |
if ($TimeSpan.TotalSeconds -gt 0) { | |
$PrettyString += "$($TimeSpan.Seconds) seconds, " | |
} | |
if ($TimeSpan.TotalMilliseconds -gt 0) { | |
$PrettyString += "$($TimeSpan.Milliseconds) milliseconds" | |
} | |
$TimeSpan | Add-Member -MemberType "NoteProperty" -Name "PrettyString" -Value $PrettyString | |
return $TimeSpan | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment