Adding Style to PowerShell HTML Reports

Over the last few articles, I have demonstrated some HTML tips and tricks for creating HTML reports in PowerShell. We are going to pick up where we left off. If you are just jumping in, take a few minutes to get caught up. I am going to use the same event log data as my source.



In the previous file, I used a CSS that included this entry:

However, my final files never had any green text. My original intention was that the date information at the end of the report would be the footer. In order for the style to apply, the final HTML code needs to look like this:

Because this is post-content, all I need to do is use a value like this for the -PostContent parameter:

Here is the complete hashtable of parameters that I am going to splat to Convertto-HTML:

The formatted footer (Image credit: Jeff Hicks)
The Formatted Footer (Image credit: Jeff Hicks)

That is pretty easy because we can control the final HTML. What about HTML when it is auto-generated? This takes a little more work on your part.

If you recall, I decided that with my report, I wanted any log files with 0 entries to be displayed in red. I might as well show you how to highlight the entire row, as well as just the value. In both cases, I need an HTML fragment for the main body and I am going to instruct PowerShell to treat it as an XML document.

Now the fun part. I need to loop through each table row and check the value of the entry count.

I am starting my loop at 1 because the table header is row 0. I will keep looping for every table row in the table. For each row, I check the value of the 4th table column, (starting at 0 remember?) which is the column with my entry count. If the value is equal to 0, I will create the class attribute and append it to the column entry. The final results are in the InnerXML property.

Dynamically modified HTML (Image credit: Jeff Hicks)
Dynamically Modified HTML (Image credit: Jeff Hicks)

I need to make sure I define this style.

I already have the HTML body, so I will not be piping $data to Convertto-HTML. Instead, I will add it as a parameter and splat the hashtable.

Colorized output (Image credit: Jeff Hicks)
Colorized Output (Image credit: Jeff Hicks)

Well, almost. You can see that the 0 entries are now in red but the post and pre-content gets a bit mangled when using this technique. No worries, I wanted to demonstrate the use of fragment anyway.

With this, I can assemble a document from a number of fragments:

To highlight the entire row in red, append the class to the row, instead of the table cell:

Highlighted rows (Image credit: Jeff Hicks)
Highlighted Rows (Image credit: Jeff Hicks)

Next time, I still have a few more tricks to show you. For now, I hope you will try these tips out. Let me know what you think

Related Topics:

  • PowerShell

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