Summary: Microsoft Scripting Guy, Ed Wilson, shows a script that produces red for down and green for up. 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 blog in a series about using the Convertto-HTML cmdlet and creating a HTML server uptime report. The first blog talked about creating an HTML uptime report, the second one used Windows PowerShell to create a report that displays uptime and disk space. The third blog discusses adding color to the uptime report. The fourth blog talks about creating a scheduled job to run the script. You should read the three previous blogs prior to today’s. 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 that 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 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 on whether the server is up. The code is shown 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 is shown 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 I create an 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 blog, 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 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 here.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