Convertto-Html: Highlight the cells with special values?

前端 未结 2 1828
醉话见心
醉话见心 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:55

    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 -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), "(.*?)")
    #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
            '{0}' -f $match.Groups[1].Value
        } else {
            #Return org. value
            $match.Value
        }
    
    }
    
    #Regex replace all lines
    $body = $html | Foreach-Object { [regex]::Replace($_, "^((?:[^<]*?<\/td>){$cpu}(\d.*?)<\/td><.*)", $ME) }
    

    Regex-demo @ Regex101

提交回复
热议问题