Enhancing HTML Reports with PowerShell

Posted on August 4, 2017 by Jeff Hicks in PowerShell with

I hope you have been enjoying this little series on tips and tricks for doing more with ConvertTo-HTML and creating great looking reports in PowerShell. Today, I want to continue where we left off. I am assuming you have been following along. If not, take some time to start at the beginning or you may be a bit lost.

 

 

Often, I create reports from a number of HTML fragments. Usually, I have multiple pieces of information that I want to display. Perhaps, I want to parse the HTML, so I can do things, such as dynamically add style classes as I did last time. I am going to repeat that process today by reusing the core code from the previous article. In addition to event log information, I want to get some other system information as well. Let’s begin with it empty:

The first thing I want to do is embed a graphic file in the document. The easy way would be to use the <IMG> tag and specify the path to a graphic file. However, I like making my HTML files portable or self-contained. The first step is to convert the file to a Base64 string.

When I create the IMG tag, I want to include an ALT attribute for the file name. I also need to know the file type.

All that remains is to create the IMG tag.

The only thing you really need is the src value. You can experiment with style settings or other IMG attributes. Since I want the graphic at the top of the file, I will add it to the array of fragments.

I know that I am going to include some operating system information, so I might as well add a header for it.

I plan on using Get-CimInstance to retrieve operating system information. I only need a few properties converted to an HTML fragment.<p>

You should notice that I did something different here. By default, fragments are created as HTML tables. For this section, I wanted a list. You can mix and match lists and tables as you see fit.

Next up is some system information, which will come from this function:

Remember, you always want to convert objects to HTML. In this situation, I want the system information displayed as a list too.

From here, I will reuse my code from last time to get and process event log information, highlighting empty logs with red text. I will also include my standard footer.

Sponsored

The last part is to define my hashtable of parameters for ConvertTo-HTML and create the final document.

I moved the computer name to the report title. Here is the final result.

A mixed HTML report built with PowerShell (Image Credit: Jeff Hicks)

A Mixed HTML Report Built with PowerShell (Image Credit: Jeff Hicks)

Of course, all of this code is something you would put into a script or function to make it easier to use. Not that the example I have been working with is especially compelling, but I hope you will pay more attention to the techniques I used. That is the real takeaway. If you get stuck with your own PowerShell scripts creating HTML reports, I encourage you to use the PowerShell forums.

Sponsored

Before we leave this topic, I want to come back one more time and show you a technique I think you will find useful.

Sponsored

Tagged with , ,

Register for this Webinar