Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to create a color HTML server uptime report.
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
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.
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
0 comments