{"id":8311,"date":"2012-08-12T00:01:00","date_gmt":"2012-08-12T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/08\/12\/weekend-scripter-color-coded-uptime-report\/"},"modified":"2012-08-12T00:01:00","modified_gmt":"2012-08-12T00:01:00","slug":"weekend-scripter-color-coded-uptime-report","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-color-coded-uptime-report\/","title":{"rendered":"Weekend Scripter: Color-Coded Uptime Report"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, shows a script that produces red for down and green for up.\nMicrosoft 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 &ndash; I am teaching a Windows PowerShell class in Georgetown Kentucky, and&nbsp; I am speaking at&nbsp;<a href=\"http:\/\/www.powershellgroup.org\/cincinnati.oh\" target=\"_blank\">the first ever Cincinnati Windows PowerShell User Group<\/a>. 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.<\/p>\n<h2>Frankenscript for sure&mdash;but it works<\/h2>\n<p style=\"padding-left: 30px\"><b>Note<\/b>&nbsp;&nbsp;&nbsp;This is the fifth blog in a series about using <b>the Convertto-HTML<\/b> cmdlet and creating a HTML server uptime report. The&nbsp;<a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/08\/07\/use-powershell-to-create-an-html-uptime-report.aspx\" target=\"_blank\">first blog talked about creating an HTML uptime report<\/a>, the second one&nbsp;<a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/08\/08\/use-powershell-to-create-a-report-displaying-free-disk-space.aspx\" target=\"_blank\">used Windows PowerShell to create a report that displays uptime and disk space<\/a>. <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/08\/09\/use-powershell-to-create-a-color-server-uptime-report.aspx\">The third blog discusses adding color<\/a>&nbsp;to the uptime report. The fourth blog talks about&nbsp;<a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/08\/11\/weekend-scripter-use-the-windows-task-scheduler-to-run-a-windows-powershell-script.aspx\" target=\"_blank\">creating a scheduled job to run the script<\/a>. You should read the three previous blogs prior to today&rsquo;s.\nI will admit it. I am not a web developer. Today&rsquo;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&mdash;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&hellip;\nThe 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.<\/p>\n<p style=\"padding-left: 30px\">$style = @&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&lt;style&gt;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;BODY{background-color:AntiqueWhite;}<\/p>\n<p style=\"padding-left: 30px\">&nbsp;TABLE{border-width: 1px;border-style: solid;border-color: Black;border-collapse: collapse;}<\/p>\n<p style=\"padding-left: 30px\">&nbsp;TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:DarkSalmon}<\/p>\n<p style=\"padding-left: 30px\">&#8220;@<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;$precontent = @&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&lt;h1&gt;Server Uptime Report&lt;\/h1&gt;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&lt;h2&gt;The following report was run on $(get-date)&lt;\/h2&gt;<\/p>\n<p style=\"padding-left: 30px\">&#8220;@<\/p>\n<p style=\"padding-left: 30px\">&nbsp;$i = 0\nFor each server that is passed to the script, I call the <b>Get-Uptime<\/b> function and store the returned object in a <b>$uptime<\/b> variable. I also create a variable named after the server and an incremental number. I use the&nbsp;<b>PassThru<\/b><i>&nbsp;<\/i>parameter to return the variable that I create. This variable will hold the HTML fragment for later use. The code is shown here.<\/p>\n<p style=\"padding-left: 30px\">Foreach( $server in $servers)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; $uptime = Get-UpTime -servers $server<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; $rtn = New-Variable -Name &#8220;$server$i&#8221; -PassThru\nNow if the&nbsp;<i>uptime&nbsp;<\/i>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.<\/p>\n<p style=\"padding-left: 30px\">if ($uptime.uptime -eq &#8220;DOWN&#8221;)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $upstyleRed = $style + &#8220;`r`nTD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:Red} &lt;\/style&gt;&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $rtn.value = $uptime | ConvertTo-Html -As Table -Fragment&nbsp; -PreContent $upstyleRed | Out-String<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;\nIf the server is up, I add a TD element to the style with a green background, and I create an HTML fragment from the <b>Get-Uptime<\/b> results and store it in the variable created with the server name and the fragment number.<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;else<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; {&nbsp;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $upstyleGreen = $style + &#8220;`r`nTD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:Green} &lt;\/style&gt;&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $rtn.value = $uptime | ConvertTo-Html -As Table -Fragment&nbsp; -PreContent $upstyleGreen | Out-String\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;\nI now add the HTML fragment to the array of fragments as seen here.<\/p>\n<p style=\"padding-left: 30px\">&nbsp;[array]$frags+=$rtn.Name<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; $i++\nI now need to take the array of fragment names, and expand the&nbsp;<b>value<\/b><i>&nbsp;<\/i>property from the variables. I store the returned HTML code in the <b>$fragments<\/b> variable.<\/p>\n<p style=\"padding-left: 30px\">$fragments = foreach($f in $frags) { Get-Variable $f | select -ExpandProperty value }<\/p>\n<p style=\"padding-left: 30px\">I now use the fragments stored in the <b>$fragments<\/b> variable and the precontent created earlier, and pass it all to the <b>Convertto-HTML<\/b> cmdlet. This code is similar to code we used earlier in the week.<\/p>\n<p style=\"padding-left: 30px\">ConvertTo-Html -PreContent $precontent -PostContent $fragments &gt;&gt; $path<\/p>\n<p style=\"padding-left: 30px\">Invoke-Item $path&nbsp;\nAs 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.\nKeep in mind, the &lt;style&gt; 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.\nI uploaded the complete script to the Script Repository.&nbsp;<a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/HTML-Red-Green-Server-73c57eab\" target=\"_blank\">You can obtain the script here.<\/a>I invite you to follow me on&nbsp;<a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a>&nbsp;and&nbsp;<a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at&nbsp;<a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the&nbsp;<a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.\n<strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":597,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,30,61,45],"class_list":["post-8311","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-web-pages-and-htas","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/8311","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/597"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=8311"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/8311\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=8311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=8311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=8311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}