Creating Simple PowerShell HotFix HTML Reports

powershell hero

Recently I went through a series of articles building a hot fix reporting tool. If I recall correctly, part of the original forum post was about creating reports and I think that meant HTML reports. Well, even if it didn’t, IT pros (or maybe their managers) love HTML reports. PowerShell makes this a relatively simple task with ConvertTo-HTML.  With this cmdlet and the hot fix function, it is pretty easy to create an html report.

Don’t forget you still need to save the results to a file.

bare HTML hotfix report
bare HTML hotfix report (Image Credit: Jeff Hicks)

The report is pretty bare bones. But if you look at help for ConvertTo-HTML, you’ll see that you can specify a path to a CSS file.

a more colorful HTML report
a more colorful HTML report (Image Credit: Jeff Hicks)

If you are saving the file to a network share or to an intranet server, as along as the CSS file is available to everyone this works pretty well. But I wouldn’t want to have to type that long command everytime I needed a report. Or, suppose this was a monthly task and I was out of the office. I don’t want to have to rely on someone else to type the command correctly. This is where a PowerShell script is useful. All I need to do is copy and paste the command into a .ps1 file and run it. But since I’m going to go the effort of creating a script file, I might as well create something meaningful.

Let’s say that every month I need to run a report that shows all hot fixes installed in the last 45 days on all my servers.  The 45 day mark is arbitrary and I want to allow the option to search for a different number of days to satisfy the boss’ whims. I also know the file name will always be the same, but there may be times I need a different path. I can create a script with parameters to meet these requirements.

The script will always process the same computers pulled from a CSV file, so that will be the first line of my script.

I probably should have some error handling, and this could also have been a parameter, but for the sake of demonstration will go with this.

Now the script is ready to get hot fix data. Based on the simple tests, I want a separate table for each computer. This will require me to group the results and create HTML fragments for each computer.

Now for the HTML part. I need to convert each group of hotfixes into an HTML table fragment. Since I’m showing the computername and the number of hotfixes as a heading, I don’t need to include the computername in the output.

The $fragments variable is now the collection of HTML code. Personally, I like to embed the CSS style into the document to make it portable, so my script is going to define a head section as well as a footer with the report date.

All that remains is to create the HTML report.

Here’s the final result:

A grouped HTML report
A grouped HTML report (Image Credit: Jeff Hicks)

So I show the finished report to the boss who gives me a new requirement that Security Updates should be highlighted. I also realize that the online link is a url. So wouldn’t it be nice to be able to click on it to learn more about a given hotfix? These changes will require me to modify the HTML code on the fly. There’s nothing in the Convertto-HTML cmdlet that will help.

To highlight the security update, I can change the font color to red is the description matches ‘Security Update’. I can do this by inserting a class with a corresponding style entry.

Modifying the HTML to detect and insert is a bit trickier. The trick I use is to save the fragment as XML.

With an XML document, it is easier to find things using an XPath query and then modify the matching nodes. In my case, that means inserting the class attribute.

The XPath query is looking for td elements with a text value of ‘Security Update’. I can do something similar with the online url.

In this case I am replacing the text with an html link. The last step for this trick to work is to replace some characters in the InnerXML property.

The $Fragments variable is now HTML code and the rest of the script is the same, but with better results.

html hotfix report with highlights and links
html hotfix report with highlights and links (Image Credit: Jeff Hicks)

That seems pretty snazzy to me! Here’s the complete script:

But you know, one thing that I find annoying with this report, and hot fixes in general, is that I don’t know what problem the hot fix solving. If I click an online link I can read an article. What would be really handy would be to display the article title in the report. Wouldn’t that be useful? But it is tricky and not something I want to start now so check back later and we’ll wrap this up with an advanced HTML hotfix report.

Related Topics:

  • PowerShell

    Don't have a login but want to join the conversation? Sign up for a Petri Account


    More Articles by Jeff Hicks