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

  • 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...
  • Zenoss Core 4 Installation
    Zenoss Core 4 was recently released, so I thought I would document a sample installation process for it. My single server installation clo...
  • 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...
  • 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 ...
  • Measure Internet Connection Speed from the Linux Command Line
    This is a sample solution of how to test the Internet connection speed for an Amazon Web Services (AWS) EC2 Linux instance from the command ...
  • Install and configure GNS3 with TunTap on the Mac
    [ 10/30/2013 : Post has been updated to reflect configuration with GNS3 v0.8.6 and Mac OS X 10.9 (Mavericks) ] A virtual network has many ...
  • First Post!!!
    I thought I would submit my solution for the FizzBuzz test. I've used PowerShell and VBScript for the solution since the majority of m...
  • SSH using Public-Key Authentication
    Lately, I've been implementing a solution to make SSH connections more secure and manageable (i.e., getting away from password authentic...
  • Configure an Active Directory authoritative time source
    One of the most fundamental tasks in a network is to keep the clocks on all computers (and network devices) synchronized with world time. Th...
  • 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...

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