Creating Portable HTML in PowerShell

Posted on July 27, 2017 by Jeff Hicks in PowerShell with

In a previous Petri.com article, we were exploring ways to do more with HTML in PowerShell. At the end, I showed you a finished file that used a path to a local copy of a CSS file. There is nothing wrong with this if the file will never leave your computer or when testing. To make the document portable, you can embed the style information directly into your HTML file.

 

 

To keep my code samples a bit easier to read, I am going to be working with the same data.

I will pipe $data to ConvertTo-HTML. I am also going to start using a hashtable of parameter values to splat to ConvertTo-HTML.

The style information goes in the html header. I will copy the code from my CSS file to a here string variable. Then, I will add the variable to the parameter hashtable, since I have already created it.

Let’s try it out.

Using embedded CSS (Image credit: Jeff Hicks)

Using Embedded CSS (Image credit: Jeff Hicks)

Pretty good. Although, if you look closely, you will see that I have lost my report title. When you use a header, the -Title parameter is ignored. Let’s revise the header to insert the Title tag and try again.

Corrected header with title (Image credit: Jeff Hicks)

Corrected Header with Title (Image credit: Jeff Hicks)

And while we are on the subject of style, here is a tip on how to get alternating bands. This is helpful for very long tables. Insert this code into your style sheet.

You can set the background-color value to any valid HTML color value. Another style trick is to set the table to fill more of the page and also to automatically resize when you resize the browser.

Here is my final complete code:

A better formatted report (Image credit: Jeff Hicks)

A Better Formatted Report (Image credit: Jeff Hicks)

This is a much nicer looking report and I hope you will try the code out for yourself to see this in action.

Sponsored

My sharp eyed readers may have noticed a section of the style sheet that defined a footer. However, the final result does not appear to be using it. How is that supposed to work? Or let’s say I want any event log with 0 entries to display in red. How can I do that? I will show you how in the next article.

Sponsored

Tagged with ,

Register for this Webinar