One of the most wonderful things about PowerShell is the ability to pass objects down the Pipe for further processing. In my first version of Get-Uptime I did not utilize this because I wanted to show what a huge difference it makes. I wanted to get this out pretty quick so I didn’t add the prettiness that I had in my first version, but it is easy enough to add.

Here is the new Code. After the code I give some examples of how to use it.

Function Get-Uptime{
    Param([string]$server)
    Begin {
        function PingServer {
            Param([string]$srv)
            $pingresult = Get-WmiObject win32_pingstatus -f "address=’$srv’"
            if($pingresult.statuscode -eq 0) {$true} else {$false}
        }
        function myUptime {
            param([string]$srv)
            $os = Get-WmiObject Win32_OperatingSystem -ComputerName $srv
            $uptime = $os.LastBootUpTime
            return $uptime
        }
        function ConvertDate {
            param([string]$date,[string]$srv)
            $year = $date.substring(0,4)
            $Month = $date.Substring(4,2)
            $day = $date.Substring(6,2)
            $hour = $date.Substring(8,2)
            $min = $date.Substring(10,2)
            $sec = $date.Substring(12,2)
            $RebootTime = new-Object System.DateTime($year,$month,$day,$hour,$min,$sec)
            $now = [System.DateTime]::Now
            $uptime = $now.Subtract($RebootTime)
            $uptimeval = "$($uptime.days) days, $($uptime.Hours) hours, $($uptime.Minutes) minutes, $($uptime.seconds) seconds"
            $lastReboot = $rebootTime.toString()
            $sObject = new-Object -typename System.Object
            $sObject | add-Member -memberType noteProperty -name ServerName -Value $srv
            $sObject | add-Member -memberType noteProperty -name Days -Value $uptime.days
            $sObject | add-Member -memberType noteProperty -name Hours -Value $uptime.Hours
            $sObject | add-Member -memberType noteProperty -name Minutes -Value $uptime.Minutes
            $sObject | add-Member -memberType noteProperty -name Seconds -Value $uptime.seconds
            $sObject | add-Member -memberType noteProperty -name uptime -Value $uptimeval
            $sObject | add-Member -memberType noteProperty -name LastReboot -Value $rebootTime.ToUniversalTime()
            $sObject | add-Member -memberType noteProperty -name LastRebootUtc -Value $rebootTime.ToFileTimeUtc()
            write-Output $sObject
        }
        Write-Host
        $process = @()
        $objCollection = @()
    }
    Process {
        if($_){
            if($_.ServerName ){
                $process += $_.ServerName
            }
            else{
                $process += $_
            }
        }
    }
    End {
        if($Server){$process += $server}
        $i = 1
        foreach ($Server in $process){
            write-progress $Server "Total Progress->" -percentcomplete ($i/$process.length*100)
            if(PingServer $server){
                $result = myUptime $server
                $srvObject = ConvertDate $result $server
                $objCollection += $srvObject
            }
            else {
                Write-Host "Server [$server] not Pingable" -foregroundcolor red
            }
            $i = $i+1
        }
        Write-Output $objCollection
        Write-Host
    }
}

Examples:

This get uptime on a Single Server

get-uptime server | %{$_.uptime}

This gets all servers up for more than 30 days

$sl | Get-Uptime | ?{$_.Days -gt 30} | %{write-host "$($_.ServerName) :: $($_.uptime)"}

This Displays the Last Reboot Time of a list of servers.

$sl | get-uptime | %{Write-Host "Server $($_.ServerName) rebooted on $($_.LastReboot)"}

The part of this I want to focus on is this

$sObject = new-Object -typename System.Object
 $sObject | add-Member -memberType noteProperty -name ServerName -Value $srv
 $sObject | add-Member -memberType noteProperty -name Days -Value $uptime.days
 $sObject | add-Member -memberType noteProperty -name Hours -Value $uptime.Hours
 $sObject | add-Member -memberType noteProperty -name Minutes -Value $uptime.Minutes
 $sObject | add-Member -memberType noteProperty -name Seconds -Value $uptime.seconds
 $sObject | add-Member -memberType noteProperty -name uptime -Value $uptimeval
 $sObject | add-Member -memberType noteProperty -name LastReboot -Value $rebootTime.ToUniversalTime()
 $sObject | add-Member -memberType noteProperty -name LastRebootUtc -Value $rebootTime.ToFileTimeUtc()

This is where I define my custom object. There are numerous ways to do this, but I chose this way. Notice the use of add-member cmdlets… It is extremely powerfull and extremely easy to use.

Basically what I do is create a Generic Object $sObject. Add some noteProperties and populate them. Very simple, but as you can very “POWER”ful.

Lee Holms has some excelent information on Custom Objects here:

http://www.leeholmes.com/blog/AddCustomMethodsAndPropertiesToTypesInPowerShell.aspx

I hope you enjoyed this new version… I currently actually keep both versions in my functions.ps1 file that I load in my profile. One as Get-Uptime and one as Get-UptimeExt. I get pretty and Smart :)