ca9163d9
ca9163d9

Reputation: 29199

Convertto-Html: Highlight the cells with special values?

The following statement will generate a bit html table.

ps | convertto-html

I want to make the text (or the whole line) of "CPU(s)" in color red (add css class to the <td>) if the value is greater than, saying, 100? The html code will be used as email body so javascript is not an option.

Upvotes: 2

Views: 6567

Answers (2)

bluuf
bluuf

Reputation: 1001

Sample I once made at a late night while playing around with Powershell (note : code is not optimized but it can be used as inspiration since it does exactly what you want). This sample was based on the Exchange CMDlets where I wanted to highlight mailboxes which were over XXX GB.

$MailboxStatisticsList = Get-Mailbox | Get-MailboxStatistics
$Report = foreach ($Mailbox in $MailboxStatisticsList)
    {
    $Split = $Mailbox.TotalItemSize.Split(" ")
    ## New way to get it right : we Regex the bytes and convert it to MB
    $MailboxItemSize = $($Split[2] -replace '[^0-9]') / 1MB
    [PSCustomObject] @{
        Mailbox = $Mailbox.DisplayName
        TotalItemSize = $MailboxItemSize -as [int]
        } ## END OF PSCUSTOMOBJECT
    } ## END OF FOREACH LOOP


$CSS = @'
<style>
body {
    font-size : 14px;
    font-family : arial;
    }

h2 {
    text-align: center;
    }

div.topbar {
    height : 50px;
    width : 100%;    
    background-color : darkred;
    }

table.mailboxsizetable {
    background-color : lightblue;
    border : 3px solid black;
    border-radius : 5px;
    box-shadow: 10px 10px 5px #888888;
    }

td {
    border : 2px solid black;
    border-radius : 5px;
    }

tr.tablerow {
    background-color : lightgreen;
    }

th {
    background-color : yellow;
    border : 3px solid black;
    border-radius : 5px;
    font-size : 16px;
}

td.toobig {
    background-color : red;
    color : green;
    }

div.tablediv table {
    margin : auto;
    }

td:hover {
    background-color : white;
    color : gold;
    font-weight : bold;
    }

</style>
'@

$BaseHTML = @'
<div class="topbar">
</div>
<h2>Service Report</h2>
'@

## Convert to HTML and export table to a variable
$HTMLTable = $Report | Sort-Object TotalItemSize -Descending | ConvertTo-Html -Fragment
## Import the HTML table as XML
[xml]$XML = $HTMLTable
## Create the attribute Class....
$TableClass = $XML.CreateAttribute("class")
## ....and give it the value "test"
$TableClass.Value = "mailboxsizetable"
## Now we stick it together and append it
$XML.table.Attributes.Append($TableClass)
## Outputting $XML.OuterXML returns the HTML with the class

## Now we take it 1 step further : conditional formatting for the table rows (on individual <td> is on my ToDo list)
## Foreach TR :
foreach ($TableRow in $XML.table.SelectNodes("tr"))
    {
    ## each TR becomes a member of class "tablerow"
    $TableRow.SetAttribute("class","tablerow")
    ## If row has TD and TD[1] has the state running...
    if (($TableRow.td) -and ([int]$TableRow.td[1] -gt 2000))
        {
        ## tag the TD with the class "notrunning" (should make this an Id)
        $TableRow.SelectNodes("td")[1].SetAttribute("class","toobig")
        }
    }
## Added code : enclose the table in a div tag
$FinalHTMLTable = [string]::Format('<div class="tablediv">{0}</div>',$XML.OuterXml)
ConvertTo-Html -Head $CSS -Body ($BaseHTML + $FinalHTMLTable) | Out-File C:\TempFolder\mailexport.html

Upvotes: 1

Frode F.
Frode F.

Reputation: 54931

You could use regex-replace with a MatchEvaluator to find the rows with values above 100 and add style="color: #FF0000;" to the row to make the text red.

You can modify the regex + matchevaluator if you only want the CPU-value to be red by adding the color-style to the <td>-tag for the CPU-field.

Ex.

#Get HTML
$html = ps | select name, cpu, Handles, FileVersion | convertto-html

#Get headers
$headers = [regex]::Matches(($html | out-string), "<th>(.*?)</th>")
#Find index of CPU-header
$cpu = $headers | ForEach-Object -Begin { $i = 0 } -Process { if($_.Groups[1].Value -eq 'CPU') { $i } else { $i++ } }

#Regex Replace MatchEvaluator
$ME = {
    param($match)

    #If Group 2 (CPU) is greater than 100
    if([double]::Parse($match.Groups[2].Value) -gt 100) {
        #Add red text-style to row
        '<tr style="color: #FF0000;">{0}' -f $match.Groups[1].Value
    } else {
        #Return org. value
        $match.Value
    }

}

#Regex replace all lines
$body = $html | Foreach-Object { [regex]::Replace($_, "^<tr>((?:<td>[^<]*?<\/td>){$cpu}<td>(\d.*?)<\/td><.*)", $ME) }

Regex-demo @ Regex101

Upvotes: 2

Related Questions