I created a PowerShell advanced function that emulates the uptime
command for Unix-like operating systems.
The uptime
command displays the current time, the length of time the system has been up, the number of users, and the load average of the system over the last 1, 5, and 15 minutes.
uptime
output:
21:33 up 7 days, 11:10, 2 users, load averages: 0.05 0.08 0.08
My function returns a custom PowerShell object, so we have the option to pass it to the pipeline for further processing and/or formatting.
Get-Uptime | Get-Member
output:
TypeName: BN.Uptime
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Computer NoteProperty System.String Computer=DC01
Days NoteProperty System.Int32 Days=9
Hours NoteProperty System.Int32 Hours=12
Minutes NoteProperty System.Int32 Minutes=46
Seconds NoteProperty System.Int32 Seconds=16
Here is the function:
|
|
Usage
- Get uptime information for multiple computers.
'dc01','db01','sp01' | Get-Uptime | ft -auto comp*,days,hours,min*,sec*
output:
Computer Days Hours Minutes Seconds
-------- ---- ----- ------- -------
DC01 17 7 3 58
DB01 17 4 4 52
SP01 17 3 55 04
- Get uptime information for multiple computers from Active Directory.
ipmo ActiveDirectory
Get-Uptime -cn (Get-ADComputer -f * | select -expand name) | ft -auto comp*,days,hours,min*,sec*
output:
Computer Days Hours Minutes Seconds
-------- ---- ----- ------- -------
DC01 17 7 3 58
DB01 17 4 4 52
SP01 17 3 55 04
- Get uptime information for local computer in Unix-like format.
function uptime
{
$time = [System.DateTime]::Now.ToShortTimeString()
$uptime = Get-Uptime
Write-Host $time " up" $uptime.days "day(s)," $uptime.hours "hour(s)," $uptime.minutes "min(s)"
}
uptime
output:
4:53 PM up 9 day(s), 23 hour(s), 30 min(s)