Last active
September 5, 2015 17:43
-
-
Save lipkau/f7cf55280cd6ad3c6ffa to your computer and use it in GitHub Desktop.
Get Computer's uptime
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 | |
Get Computer's uptime | |
.DESCRIPTION | |
Returns the total time since the last reboot | |
.NOTES | |
AUTHOR : Oliver Lipkau <[email protected]> | |
VERSION: 1.0.0 - OL - Initial Code | |
.PARAMETER ComputerName | |
Hostname/IP of the remote computer | |
.PARAMETER Credential | |
Authentication Credentials for the remote computer | |
.INPUTS | |
System.String | |
System.Management.Automation.PSCredential | |
.OUTPUTS | |
System.TimeSpan | |
#> | |
[CmdletBinding()] | |
[OutputType([TimeSpan] )] | |
param( | |
[string]$ComputerName = "localhost", | |
[System.Management.Automation.PSCredential]$Credential | |
) | |
begin { } | |
process { | |
if ($Credential) | |
{ | |
$time = Get-WmiObject -class Win32_OperatingSystem -computer $ComputerName -Credential $Credential | |
} | |
else | |
{ | |
$time = Get-WmiObject -class Win32_OperatingSystem -computer $ComputerName | |
} | |
$t = $time.ConvertToDateTime($time.Lastbootuptime) | |
New-TimeSpan $t $(get-date) | |
} | |
end { } | |
} | |
$upTime = Get-Uptime | |
"$($upTime.days)d $($upTime.hours)h $($upTime.minutes)m $($upTime.seconds)S" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output for
"$($upTime.days)d $($upTime.hours)h $($upTime.minutes)m $($upTime.seconds)S"