Summary: Microsoft Scripting Guy Ed Wilson shows a script that produces red for down and green for up
Weekend Scripter: Color coded uptime report
Microsoft Scripting Guy, Ed Wilson, is here. Well, I have been playing around with this script off and on all week. Last night, my old high school friend John called to discuss plans for our upcoming Kentucky trip – I am teaching a Windows PowerShell class in Georgetown Kentucky, and I am speaking at the first ever Cincinnati Windows PowerShell User Group. The cool thing about the Cincinnati Windows PowerShell User Group is the Scripting Wife talked to Mike (who is starting the group) when we were in Columbus Ohio during the PowerShell Saturday event. We are both looking forward to helping to launch the Cincinnati Windows PowerShell User Group.
Frankenscript --- for sure – but it works
NOTE: This is the fifth article in a series of articles about using the Convertto-HTML cmdlet and creating a HTML server uptime report. The first article talked about creating an HTML Uptime Report, the second one used PowerShell to create a report that displays disk space as well as uptime. The third article discusses adding color and stuff to the uptime report. The forth article talks about creating a scheduled job to run the script. You should read the three previous articles prior to today’s article.
I will admit it; I am not a web developer. Today’s script will prove that beyond any shadow of a doubt. The script works, but it is not pretty, and I am certain there has to be a better way. In fact, I toyed with adding a bit of J script to the page to do some logic, but that did not work really well because Internet Explorer blocks active content by default, and it was an extra step. In the end, the script I came up with does not display properly in Internet Explorer any way … at least not in the beta version in my version of Windows 8. But the code works, because the page does display properly in Microsoft Word --- strange. Oh well.
The entry point to the script uses two here strings to build the style and the precontent. I left the TD style (for the table detail) because I add that line based upon whether the server is up or not. The code appears here.
$style = @"
<style>
BODY{background-color:AntiqueWhite;}
TABLE{border-width: 1px;border-style: solid;border-color: Black;border-collapse: collapse;}
TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:DarkSalmon}
"@
$precontent = @"
<h1>Server Uptime Report</h1>
<h2>The following report was run on $(get-date)</h2>
"@
$i = 0
For each server that is passed to the script, I call the Get-Uptime function and store the returned object in a $uptime variable. I also create a variable named after the server and an incremental number. I use the passthru parameter to return the variable that I create. This variable will hold the HTML fragment for later use. The code appears here.
Foreach( $server in $servers)
{
$uptime = Get-UpTime -servers $server
$rtn = New-Variable -Name "$server$i" -PassThru
Now if the uptime property is equal to down I create add the TD style with the color of red to my style and I create a HTML fragment using that style, and the information returned from the Get-Uptime function. I store the returned HTML fragment in the value property of the variable created with the Server name and the fragment number. This code appears here.
if ($uptime.uptime -eq "DOWN")
{
$upstyleRed = $style + "`r`nTD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:Red} </style>"
$rtn.value = $uptime | ConvertTo-Html -As Table -Fragment -PreContent $upstyleRed | Out-String
}
If the server is up, I add a TD element to the style with a Green background, and create a HTML fragment from the Get-Uptime results and store it in the variable created with the Server name and the fragment number.
else
{
$upstyleGreen = $style + "`r`nTD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:Green} </style>"
$rtn.value = $uptime | ConvertTo-Html -As Table -Fragment -PreContent $upstyleGreen | Out-String
}
I now add the HTML fragment to the array of fragments as seen here.
[array]$frags+=$rtn.Name
$i++
I now need to take the array of fragment names, and expand the value property from the variables. I store the returned HTML code in the $fragments variable.
$fragments = foreach($f in $frags) { Get-Variable $f | select -ExpandProperty value }
I now use the fragments stored in the $fragments variable and the precontent created earlier and pass it all to the Convertto-HTML cmdlet. This code is similar to code we used earlier in the week.
ConvertTo-Html -PreContent $precontent -PostContent $fragments >> $path
Invoke-Item $path
As I mentioned at the beginning of the article, it does not display properly in Internet Explorer, but it does display properly in Microsoft Word. Strange, but oh well. It was a fun script to write, and I hope you enjoy playing around with it.
Keep in mind, the <style> tags are being dropped right now by the Script Repository in the code. So you will need to add them in manually – until we can get the bug fixed. Hope you have a great week.
I uploaded the complete script to the Script Repository. You can obtain the script from there.
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