Skip to content

Instantly share code, notes, and snippets.

@lipkau
Last active September 5, 2015 17:43
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 lipkau/f7cf55280cd6ad3c6ffa to your computer and use it in GitHub Desktop.
Save lipkau/f7cf55280cd6ad3c6ffa to your computer and use it in GitHub Desktop.
Get Computer's uptime
function Get-Uptime
{
<#
.SYNOPSIS
Get Computer's uptime
.DESCRIPTION
Returns the total time since the last reboot
.NOTES
AUTHOR : Oliver Lipkau <oliver@lipkau.net>
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"
@lipkau
Copy link
Author

lipkau commented Sep 5, 2015

Output for "$($upTime.days)d $($upTime.hours)h $($upTime.minutes)m $($upTime.seconds)S"

1d 23h 3m 20S

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment