Friday Fun: 50 Shades of PowerShell HTML Reports

happyreport I’ve been working on a project for a client that includes creating an HTML report, generated by PowerShell. I originally thought I would include a certain feature but decided against it. However, this is so cool I thought I’d share it with you as a Friday Fun article. I’ve done alot this year with some advanced HTML scripting techniques and this one might come in handy.

I’m always looking for ways to add visual reinforcement to my HTML reports. And since keeping track of disk space is a common IT Pro task, I figured it would be nice to have a visual representation on disk utilization. So I created a short proof of concept script that generates an HTML report like this:


What do you think? Here’s how I did it.

The key element here is the addition of a gradient. In the script you can see that I’ve defined a here string for the gradient. The string includes code to support just about any browser, that’s why you see all the background-image lines. The here string also has place holders, {0} and {1} for the starting and ending percentages. I’ll explain how that works in a moment.

Using Get-CIMInstance, the script gets fixed logical disks. Now, instead of simply creating an HTML fragment, I create the fragment as an XML document. This allows me to add a caption to the table, using the computer name. Then I iterate through the table node, skipping the first row which is the table header. I create an attribute called Style.

Next, I get the value of the last cell, which is the PercentFree value. That’s one of the reasons I used an ordered hashtable so I could guarantee that the last cell would always be the PercentFree property. I grab the value and make sure it is an integer.

I can use this value and plug it in to my gradient here string using the -f operator.

I append the style attribute to each node. This allows me to set different values for each drive.

After going through the table rows, all that remains is to add the modified HTML, which is the InnerXML property to my array of fragments and create the final report.

The gradient isn’t absolute but it gives you a rough visual approximation of how much free space is on each drive. By the way, you can also use the gradient in the body element of a style sheet if you want to jazz up the background of your report.

I included plenty of comments in my code which I hope helps. If not, please leave a comment. Enjoy!

9 thoughts on “Friday Fun: 50 Shades of PowerShell HTML Reports”

    1. Glad to hear. It wouldn’t take much more work to turn this into a function that could process multiple computers and create a single report.

  1. Is this script Powershell 4.0 only as Get-CimInstance throws an error on my Windows 7 SP1 laptop which runs PowerShell v3.7.0.92 according to Get-Host ?

    I’m slightly confused as the 1st line of your script (which looks pretty cool, btw!) says -requires version 3.0 ?

    1. This does NOT require PowerShell v4 although it should run on v4. The #requires line at the beginning tells PowerShell not to run the script on anything older than PowerShell 3.0 because I’m using Get-CimInstance which was introduced in v3. But what puzzles me is your version number. Get-Host should just show 3.0. If you look at $psversiontable, what do you see for PSVersion? Or what is the error you are getting?

Comments are closed.