Joey asked a cool question on my last post on out-html: what if you wanted to colorize the output of out-html so that some rows/values were colored differently based on a criteria?
I can think of a couple of different ways to handle this, and it certainly is a nice hack. But rather than building the solution, I'll post some hints and see if the community can pull some nice stuff together :). Here are some ideas: Since the goal is to color values in any given column/row a given color based on a certain criteria, why not parameterize this as a function of the input? In simpler terms, why not let the user pass in a "rule" on how out-html should color things?
To be even more specific: add a parameter (or more than one parameter) to the script that takes a color value, a criteria which when evaluated to true will apply that color, and finally a property to which the color/criteria should apply. Here's a usage pattern:
get-mailboxstatistics | out-html -property ItemCount -criteria { $prop -lt 100 } -color "Green"
Some further things to hack: how would you add support for more than one propertr and more than one criteria? Ok, so that's one approach. How about an approach which focuses on the input before it gets to out-html? Imagine a pre-processer to out-html:
get-mailboxstatistics | apply-colors | out-html
This function in the middle would output basically the same thing as get-mailboxstatistics except it would add the appropriate formatting to values that you care about. The downside of this approach might be that the function is tied to a particular input data set. But you could combine approaches #1 and #2 to come up with some common ground. So there are some ideas on how to do this.
As usual, I'm probably complicating things too much, so if you have a simpler solution... go for it and show us how its done :) If I get some more time to tweak I'll see if I can cook up a version of out-html that supports custom coloring. Till then, happy hacking!