Convertto-Html: Highlight the cells with special values?

前端 未结 2 1832
醉话见心
醉话见心 2021-01-29 00:05

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

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-29 00:40

    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 = @'
    
    '@
    
    $BaseHTML = @'
    

    Service Report

    '@ ## 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 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('
    {0}
    ',$XML.OuterXml) ConvertTo-Html -Head $CSS -Body ($BaseHTML + $FinalHTMLTable) | Out-File C:\TempFolder\mailexport.html

提交回复
热议问题