I created a PowerShell advanced function that emulates the uptime command in 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.
My function will return a custom PowerShell object, so we have the option to pass it to the pipeline for further processing and/or formatting.
Here is the function:
Usage
The following are just a few examples of sample usage for the function.
# Get Uptime Information for Multiple Computers (Method 1)
# Get Uptime Information for Multiple Computers (Method 2)
# Get Uptime Information for Local Computer in Unix-like Format
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
21:33 up 7 days, 11:10, 2 users, load averages: 0.05 0.08 0.08
My function will return a custom PowerShell object, so we have the option to pass it to the pipeline for further processing and/or formatting.
PS> Get-Uptime | Get-Member
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:
| 001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 | function Get-Uptime { [CmdletBinding()] param ( [Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] [Alias('hostname')] [Alias('cn')] [string[]]$ComputerName = $env:COMPUTERNAME ) BEGIN {} PROCESS { foreach ($computer in $ComputerName) { try { $os = Get-WmiObject -Class Win32_OperatingSystem ` -ComputerName $computer -ErrorAction Stop $time = $os.ConvertToDateTime($os.LocalDateTime) - ` $os.ConvertToDateTime($os.LastBootUpTime) # Create property hash table for custom PS object $props = @{'Computer'=$os.CSName; 'Days'=$time.Days; 'Hours'=$time.Hours; 'Minutes'=$time.Minutes; 'Seconds'=$time.Seconds;} # Create custom PS object and apply type $uptime = New-Object -TypeName PSObject -Property $props $uptime.PSObject.TypeNames.Insert(0,'BN.Uptime') Write-Output $uptime } catch { # Check for common DCOM errors and display "friendly" output switch ($_) { { $_.Exception.ErrorCode -eq 0x800706ba } ` { $err = 'Unavailable (Host Offline or Firewall)' } { $_.CategoryInfo.Reason -eq 'UnauthorizedAccessException' } ` { $err = 'Access denied (Check User Permissions)' } default { $err = $_.Exception.Message } } Write-Warning "$computer - $err" } } } END {} } |
Usage
The following are just a few examples of sample usage for the function.
# Get Uptime Information for Multiple Computers (Method 1)
PS> 'dc01','db01','sp01' | Get-Uptime | ft -auto comp*,days,hours,min*,sec*
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 (Method 2)
PS> ipmo ActiveDirectory
PS> Get-Uptime -cn (Get-ADComputer -f * | select -expand name) | ft -auto comp*,days,hours,min*,sec*
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
PS> 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)"
>> }
>>
PS> uptime
4:53 PM up 9 day(s), 23 hour(s), 30 min(s)