Zachary Loeber

The personal website of Zachary Loeber.

Powershell: Colorize-Table Function

Here is a function I modified for quickly modifying the attributes of a table’s rows or individual cells. It uses Linq and is blistering fast. The results are pretty as well so that doesn’t hurt either.

Here is the script on the Technet Gallery

Here is an example which produces a semi-useful example report of the top memory consuming processes.  Anything taking over 150 Mb is highlighted yellow. Above 400 Mb gets highlighted red. The order for processing the table is obviously important, so they don’t all end up the same color. For this example we highlight the entire row but the individual cells can just as easily be color coded with this function (or styled or whatever fits your fancy actually).

By default the table is boring. This custom header is used to add some borders and such.

$TableStyle = @'
<title>Process Report</title>
      <style>
      BODY{font-family: Arial; font-size: 8pt;}
      H1{font-size: 16px;}
      H2{font-size: 14px;}
      H3{font-size: 12px;}
      TABLE{border: 1px solid black; border-collapse: collapse; font-size: 8pt;}
      TH{border: 1px solid black; background: #dddddd; padding: 5px; color: #000000;}
      TD{border: 1px solid black; padding: 5px;}
      </style>
'@

Now gather all processes, include the name, company, and Memory. Rename WS to Memory for readability, truncate, and convert to Mb. then sort in reverse order and return the top 5 results.

$tabletocolorize = $(get-process | select -Property ProcessName,Company,@{Name="Memory";Expression={[math]::truncate($_.WS/ 1Mb)}} | Sort-Object Memory -Descending | Select -First 5 )

This scriptblock is passed directly into the function, the variable names are important and should not change as they are used in the function (because I wasn’t able to get abstraction to work properly for whatever reason). If you really like you can replace [int]$args1 with some static number or string and the ColumnValue parameter will be ignored when passed to the function. I also found it was important to use strong typesetting in the script block. If you don’t need anything fancy and just want to change a cell/row based on what value is in it then don’t pass any scriptblock and a general equal comparison will be performed on ColumnValue which you will have to pass instead.

[scriptblock]$scriptblock = {[int]$args[0] -gt [int]$args[1]}

Run the first passthrough for any columns with the Memory value above 150 to highlight the row yellow. This is the first time through so I tack on the HTMLHead.

$testreport = Colorize-Table $tabletocolorize -Column "Memory" -ColumnValue 150 -Attr "style" -AttrValue "background:yellow;" -ScriptBlock $ScriptBlock -HTMLHead $TableStyle -WholeRow $true

Then run again for the same row for any items above 400 to highlight the row red.

$testreport = Colorize-Table $testreport -Column "Memory" -ColumnValue 400 -Attr "style" -AttrValue "background:red;" -ScriptBlock $ScriptBlock -WholeRow $true

Spit out the report to a file and open it up.

$testreport | Out-File "$pwd/testreport.html"
ii "$pwd/testreport.html"

The resulting table

ColoredTable-Process_Report

comments powered by Disqus