Posts RSS Comments RSS 130 Posts and 202 Comments till now

UPDATED!!! Get-Uptime (The Custom Object extravaganza!!!)

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

No Responses to “UPDATED!!! Get-Uptime (The Custom Object extravaganza!!!)”

  1. [...] The power of PowerShell is that you are piping objects from one cmdlet to another.  If you’re building a script, you might want the output of your script to also be objects instead of plain text so that you can pipe your script output to other scripts or cmdlets and continue to get all the powershell goodness.  The BS on PoSH blog has a great example of doing just this with a custom Get-Uptime function. [...]

  2. [...] The power of PowerShell is that you are piping objects from one cmdlet to another. If you’re building a script, you might want the output of your script to also be objects instead of plain text so that you can pipe your script output to other scripts or cmdlets and continue to get all the powershell goodness. The BS on PoSH blog has a great example of doing just this with a custom Get-Uptime function. [...]

  3. on 22 Aug 2007 at 2:46 pmTechProsaic

    [...] TrackBack: http://halr9000.com/article/417 [...]

  4. [...] Re: new script: Get-WebFile Oh, ok, here’s the code. :) function Get-WebFile { required [link] Param ( [system.uri]$srcUrl ) Begin { $webClient = new-object System.Net.WebClient $process = @() # pipeline $objCollection = @() # output objects } Process { if ($_) { # anything in the pipeline? Published Thursday, August 23, 2007 6:53 AM by microsoft.public.windows.powershell Google Group [...]

  5. on 27 Mar 2008 at 3:02 pmAlgorithms in PowerShell

    [...] http://blogs.powershellcentral.com/kscriss/2008/03/27/conditionalforcedserverrestartps1/...

Trackback this post | Feed on Comments to this post

Leave a Reply

CAPTCHA image