I decided to take a break from the XML solutions for HTML management of multi-table reports in PowerShell. This week I will demonstrate an extremely simple method that most Admin scripters can master because it uses the old standby “VBScript” to do the color tagging in the browser.
We use the same method to generate a table and create the HTML:
Same report. Same HTML.
We can colorize the rows very easily by injecting some VBScript into the output file.
The above is just a “here-string” wrapped around a block of VBScript. You can put the script into the output file manually to see it work. I built and tested the script in an HTA in about 5 minutes.
The VBScript is just a “window_onload” event that grabs first the table on the page and goes through each row in a for loop.
Set tbl1 = document.getElementsByTagName("table")(0)
For Each r In tbl1.Rows
Set cell = r.lastChild
If IsNumeric(cell.innerText) Then
If cell.innerText < 10 Then
If LCase(cell.tagName) <> "th" Then
Note how we skip the header cells and how I test for the existence of a numeric value. For each row I get the last child of the row which I know is the column with the numbers. All columns are tested against a value that tells us when to color the column red. We could also color the whole row by using cell.parent.style=”red”. We might even set the background color.
Here is a copy of the test file: Copy and run it to see that it works.
Once I had this working I just copied the script tag to my PowerShell editor and wrapped it in a here-string. I also added a variable in place of the hard coded match variable.
Combining all the pieces we get this very simple PowerShell script.
Here is a link to a sample of the report: Sample Report
Other articles in the series on managing HTML reports from PowerShell.