Use PowerShell to Create a Color Server Uptime Report

Use PowerShell to Create a Color Server Uptime Report

  • Comments 7
  • Likes

Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to create a color HTML server uptime report.

Hey, Scripting Guy! Question Hey, Scripting Guy! I hope you do not mind me writing to you—I have a rather unusual request. I like the server uptime report that you created the other day, but when I showed it to my boss, all he said was “Its rather plain, isn’t it?” I mean, how shallow. But that is another story. Personally, I thought the uptime report listed what was required, and showed it in a way that was easy to understand, but my boss thought it was plain. So, can you go back to that script and jazz it up a bit? Maybe add a little color? It would be great if you could do that. I tried, but do not see any color parameter for the ConvertTo-HTML cmdlet.

—VC

Hey, Scripting Guy! Answer Hello VC,

Microsoft Scripting Guy, Ed Wilson, is here. This morning I am listening to a German radio station. I checked the Windows 8 app store, and found a web radio app. It provided a list of radio stations, and I chose one from Germany. The Scripting Wife and I are getting ready for our trip in the fall when we are heading to Germany and other countries. We will be visiting a number of Windows PowerShell MVPs, winners of past Scripting Games, guest bloggers, and of course, many of our friends who we made when I used to teach a lot of scripting workshops in Germany and other places. Besides, the weather forecast for Munich is a lot better than the weather forecast for Charlotte, North Carolina—unless of course you like hot and humid weather—really hot and really humid weather.

VC, your request is not strange. After more than 30 years working in corporate America, I have come to realize that some bosses are sort of like ravens—attracted to anything that is bright and shiny. So to make sure your report gets their attention, you need to add a splash of color.

The secret to adding color to the Convertto-HTML cmdlet

Note   This is the third blog in a series about using the Convertto-HTML cmdlet. The first blog talked about creating an HTML uptime report, the second one used Windows PowerShell to create a report that displays disk space in addition to uptime. Today’s blog discusses adding color and stuff to the uptime report. You should read the two previous blogs prior to today’s.

VC, you are right, there is no color parameter for the Convertto-HTML cmdlet. In fact, there is a lack of a lot of stuff for the cmdlets. However, what it does is make it really easy to use HTML. The problem is that, unfortunately, when you get beyond the real basics, you need to know HTML. If you want to add some color, you need to add some style. The easiest way to do this is to create a here-string so that you do not need to worry about concatenation, quoting, and other things.

Add the sections for the style

To begin, the style section begins and ends with a style tag. The opening of the style tag is shown here.

<style>

Here is the closing of the style tag.

</style>

Inside this style section, I add information for the BODY, TABLE, the table head (TH), and the table detail (TD) portions. For the BODY, I add the background color and set it to a color from this Colors by Name MSDN page. This site on MSDN is great because it illustrates the color of each color, and it lists the name and the hex value of the color. For my sample script, I decide to make the background color AntiqueWhite. The code is shown here.

$style = @"

 <style>

 BODY{background-color:AntiqueWhite;}

Now I want to set the borders for the table. This single line of code accomplishes this task. (Note that the line may wrap on your page, but it is a single line of code, and I have added no line continuation marks.)

TABLE{border-width: 1px;border-style: solid;border-color: Black;border-collapse: collapse;}

Now, I need to set the information for the table header. I decide to make it a DarkSalmon color, but I do not change the border colors. This is shown here (once again, this is a single line).

TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:DarkSalmon}

Each row in the table is an instance of a table detail (TD), so I can use the following code to change the color of the individual rows in the table. I make it Bisque so we have a little contrast. I then close the Style  tag, and end the here-string. (The TD line of code is a single line of code.)

TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:Bisque}

 </style>

"@

There was one problem with the original script. It did not handle servers that are not online. To rectify that situation, I use the Test-Connection cmdlet to determine if the server is online. If the server is online, I obtain the uptime. When you run the Test-Connection cmdlet with the quiet switch, the cmdlet returns a Boolean value True/False, enabling a quick test case.

In addition, to ensure that the cmdlet works quickly, I change the buffersize from the default 32 bytes to 16 bytes, and I change the count from a default of 4 pings to only 1. On a well-connected network, these changes work fine. On a network with potential connectivity issues, you will need to play with the defaults. The changes in speed are significant as shown here.

PS C:\> measure-command {Test-Connection dc1 -Quiet}

 

Days              : 0

Hours             : 0

Minutes           : 0

Seconds           : 3

Milliseconds      : 61

Ticks             : 30619743

TotalDays         : 3.54395173611111E-05

TotalHours        : 0.000850548416666667

TotalMinutes      : 0.051032905

TotalSeconds      : 3.0619743

TotalMilliseconds : 3061.9743

  

PS C:\> measure-command {Test-Connection dc1 -Quiet -BufferSize 16 -Count 1}

  

Days              : 0

Hours             : 0

Minutes           : 0

Seconds           : 0

Milliseconds      : 13

Ticks             : 130932

TotalDays         : 1.51541666666667E-07

TotalHours        : 3.637E-06

TotalMinutes      : 0.00021822

TotalSeconds      : 0.0130932

TotalMilliseconds : 13.0932

The revised foreach loop is shown here.

Foreach ($s in $servers)

   {

     if(Test-Connection -cn $s -Quiet -BufferSize 16 -Count 1)

       {

        $os = Get-WmiObject -class win32_OperatingSystem -cn $s

        New-Object psobject -Property @{computer=$s;

        uptime = (get-date) - $os.converttodatetime($os.lastbootuptime)} }

      ELSE

       { New-Object psobject -Property @{computer=$s; uptime = "DOWN"} }

    } #end foreach S

To incorporate the new style into the Convertto-HTML cmdlet involves only adding the $style variable to the head parameter. This is shown here.

Get-UpTime -servers $servers |

ConvertTo-Html -head $style -As Table -body "

  <h1>Server Uptime Report</h1>

  The following report was run on $(get-date)" >> $path

 Invoke-Item $path 

The script produces a report similar to the one shown here when it runs.

Image of command output

I uploaded the complete HTML_ColorUptimeReport.ps1 script to the Scripting Guys Script Repository. You can copy the complete script there.

VC, that is all there is to fixing up the uptime report and adding a bit of color. Join me tomorrow for more Windows PowerShell cool stuff.  

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Hi Ed,

    a nice addition to what we already learned!

    The script that you uploaded to the repository is missing the enclosing <style> tags, so it displays the results not very nicely :-)

    Klaus.

  • @Ed - can you hghlight the line that say "DOWN"?  Can you make it turn red when it say DOWN?  I bet you can  after all yur name is IMMrEd which is cryptic for i am RED. Right?

    Seriously.  Can you do it?

  • @K_Schulte I will fix the script in the repository :-) ... thank you.

  • @JRV it may be possible, but I do not know of an easy way to do it. The CSS does not appear to have a cell property, or a cell object or a way to specify that. I am working on something I hope to blog about in the near future, depending on when I get it completed ... and IF it works :-) I can tell you right now, I have worked about three hours on it, and it does not work right now ... AND it is really ugly code -- maybe that is why it does not work right now :-)

  • @Ed

    CSS 3 has a firstChild and other selectors.  (not properties).  We can point to a sibling or child or parent just like in XML.

    In CSS3 it is possible to set up a system that can do this but Microsoft has yet to support all of CSS3 except in certain tools as far as I can tell.

    Here is a method that will intrigue you because it is very simple and can be extended as far as we like.  You can easily incorporate it into your current scripts in only a few lines.

    Start here: tech-comments.blogspot.com/.../powershell-dynamically-color-posh.html

    Part 2 shows the table annunciator method.

    Part 4 will show how to use CSS2/CSS3 and DOCTYPE to further enhance the the report output.

  • is it possible to add all servers in a .txt file for more easy bulk adding?

  • @Kale replace #[array]$servers = @("ser1","ser2","ser3","ser4") with [array] $servers = @(get-content C:\scripts\servers.txt) )