Binary Nature where the analog and digital bits of nature connect

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Saturday, 22 May 2010

PowerShell version of the uptime command

Posted on 21:48 by Unknown
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.

$ 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)

Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in PowerShell, Windows | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • Cisco ASA SSL VPN with Active Directory
    There is little doubt the bring-your-own-device (BYOD) strategy is becoming a popular method to access company resources. As technical prof...
  • PowerShell Function for Windows System Memory Statistics
    Memory is one of the four primary hardware resources an operating system manages. The other three are cpu, disk, and network. Analysis of sy...
  • Integrate VMware Fusion with GNS3 on your Mac
    At long last, we can finally integrate VMware Fusion with GNS3. VMware Workstation for Windows and Linux has had this capability for quite s...
  • Configure Inter-VLAN routing on a Cisco L3 Catalyst Switch
    I recently had to configure inter-VLAN routing at a client's site. I don't have to perform this task on a regular basis, so I figur...
  • SSL VPN configuration on Cisco ASA with AnyConnect VPN client
    This post will describe how to setup a Cisco Adaptive Security Appliance (ASA) device to perform remote access SSL VPN with the stand-alone ...
  • Enable sudo for RHEL and CentOS
    Sudo is an arguably safer alternative to logging in (or using the su command) to the root account. Sudo allows you to partition and delegat...
  • Get Exchange Server Version and Update Info with PowerShell
    I prefer not to "reinvent the wheel", so I spent quite a bit of time searching the web for available code that would perform the t...
  • Cisco Security Device Manager on the Mac
    Cisco Router and Security Device Manager (SDM) is a Web-based device-management tool that enables you to deploy and manage the services on a...
  • Install Request Tracker 4 on Ubuntu Server
    The CentOS6/RT4 blog post has generated terrific feedback, so I figure an Ubuntu (and Debian) distribution port is essential. The core com...
  • Install Request Tracker 4
    The argument could be made Request Tracker is the de facto standard when it comes to issue tracking systems. Maybe the only drawback of RT ...

Categories

  • AD
  • Apache
  • AWS
  • Cisco
  • Exchange
  • FFmpeg
  • GNS3
  • Linux
  • Mac
  • MariaDB
  • MySQL
  • PowerShell
  • RT
  • Security
  • SSH
  • VMware
  • Windows
  • Zenoss

Blog Archive

  • ►  2013 (8)
    • ►  October (1)
    • ►  September (1)
    • ►  August (1)
    • ►  May (1)
    • ►  April (1)
    • ►  March (1)
    • ►  February (1)
    • ►  January (1)
  • ►  2012 (3)
    • ►  December (1)
    • ►  November (1)
    • ►  April (1)
  • ►  2011 (3)
    • ►  June (1)
    • ►  May (2)
  • ▼  2010 (8)
    • ►  August (1)
    • ►  July (1)
    • ►  June (1)
    • ▼  May (1)
      • PowerShell version of the uptime command
    • ►  April (1)
    • ►  March (1)
    • ►  February (1)
    • ►  January (1)
  • ►  2009 (3)
    • ►  December (1)
    • ►  November (1)
    • ►  October (1)
Powered by Blogger.

About Me

Unknown
View my complete profile