Skip to content

Instantly share code, notes, and snippets.

@lipkau lipkau/Get-Uptime.ps1

Last active Sep 5, 2015
Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner 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
You can’t perform that action at this time.