Posts RSS Comments RSS 253 Posts and 408 Comments till now

Get-Uptime (fun with Write-Host)

I am a BIG fan of Write-Host. Think of Write-Host like wscript.Echo (vbscript) or echo (cmd.exe,) on steroids. I know I know… you want to know what I think is the best feature and funny enough… its COLOR!!! Yes, COLOR, no more two color schemes. With Write-Host you can do all sorts of colors with foreground and background.

To celebrate, I wanted to write a post illustrating just how wonderful write-host can be and I thought it would be best to use a script that I wrote. This is a script to get the uptime of server.

Function Get-Uptime {
    Param([string]$server)
    Begin {
        function Uptime {
            param([string]$srv)
            $os = Get-WmiObject Win32_OperatingSystem -ComputerName $srv
            $uptime = $os.LastBootUpTime
            return $uptime
        }
        function ConvertDate {
            param([string]$date)
            $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)
            Write-Host "==> Uptime: " -NoNewLine
            Write-Host "$($uptime.days)" -NoNewLine -ForeGroundColor Green
            Write-Host " days, " -NoNewLine
            Write-Host "$($uptime.Hours)" -NoNewLine -ForeGroundColor Green
            Write-Host " hours, " -NoNewLine
            Write-Host "$($uptime.Minutes)" -NoNewLine -ForeGroundColor Green
            Write-Host " minutes, " -NoNewLine
            Write-Host "$($uptime.seconds)" -NoNewLine -ForeGroundColor Green
            Write-Host " seconds, "
        }
        Write-Host
        $process = @()
    }
    Process {
        if($_){
            if($_.ServerName ){
                $process += $_.ServerName
            }
            else{
                $process += $_
            }
        }
    }
    End {
        if($Server){$process += $server}
        foreach ($Server in $process){
            $result = uptime $server
            Write-Host "Uptime for Server [" -NoNewline
            Write-Host $server  -NoNewline -foregroundcolor Cyan
            Write-Host "]"
            ConvertDate $result
            Write-Host
        }
        Write-Host
    }
}

As you can see I use Write-Host numerous times so let me explain a couple of the lines so you can see what is happening.

Write-Host "==> Uptime: " -NoNewLine
Write-Host "$($uptime.days)" -NoNewLine -ForeGroundColor Green
Write-Host " days, " -NoNewLine
Write-Host "$($uptime.Hours)" -NoNewLine -ForeGroundColor Green
Write-Host " hours, " -NoNewLine
Write-Host "$($uptime.Minutes)" -NoNewLine -ForeGroundColor Green
Write-Host " minutes, " -NoNewLine
Write-Host "$($uptime.seconds)" -NoNewLine -ForeGroundColor Green
Write-Host " seconds, "

What I wanted was to have the output look like uptime.exe, but I wanted to add a little flare so I decided highlighting the numbers in a different color would make it easier to read.

There are two switches that I use here -NoNewLine and -ForeGroundColor. These are pretty self explanatory, but I will explain anyway.
-NoNewLine: This tells Write-Host not to add a New Line at the end of the string.

-ForeGroundColor: This tells you the color you want the text to display in (Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta, DarkYellow, Gray, DarkGray, Blue, Green, Cyan, Red, Magenta, Yellow, White.)

There are few other useful switches I didn’t use.
-BackGroundColor: Same available colors as ForeGroundColor, but this applies to the background color. So if you want the text to be Green on Black you would do
[code]Write-Host "Hello World" -ForeGroundColor Green -BackGroundColor Black[/code]

-Separator: This applies only if you pass an array of strings to write-host. This will prepend the “separator” to each string.

write-host ("hello","this","is","Separated","by","`"-`"") -Separator "-"

As you can see Write-Host is pretty useful, especially when you need your output to be nice and neat.

OH! Before I go… there is one thing to watch out for. Although for most people write-host behaves exactly as expected when you start working with PowerShell and start understanding Pipes its important to understand that Write-Host does NOT write to the pipeline. It writes to stdout. This is very important if you are trying to write stuff the pipe. To write to the pipe you need to use write-output.

Trackback this post | Feed on Comments to this post

Leave a Reply

You must be logged in to post a comment.