HTML Conditional Formating

I’m running a report which convert to a function into an Html fragment via powershell.

here is the code
function Get-HGServices{


( [string]$Computername)

$services = Get-WmiObject -Computer $Computername Win32_Service -Credential $cred -Filter "Name = 'tlmagent' or Name =  'OpswareAgent' "

foreach($service in $services){

    $props = @{
        'Service' = $service.Caption;
        'StartMode' = $service.StartMode;
        'StateMode' = $service.State   }
    $obj = New-Object -TypeName psobject -Property $props
    write-output $obj


$services_html = Get-HGServices -ComputerName $servername |ConvertTo-EnhancedHTMLFragment -TableCssClass ‘Services’ -As Table -Properties Service, StartMode,StateMode -PreContent “Services Information” |

and here is the results

Services InformationServiceStartModeStateModeOpsware AgentAutoRunningIBM License Metric Tool and Tivoli Asset Discover AgentAutoRunning

|Service |StartMode |StateMode
|ServiceName |Auto |Running
|Servicename |Auto |Running

3 columns and 3 rows

I need to do a conditional formatting on the 3rd column per row. btw the first row is the TH.

How can i do it with powershell?

I have tried this
if($services_html -like “Auto”){$services_html = $services_html -replace “Auto” ,“Auto”}
if($services_html -like “Running”){$services_html = $services_html -replace “Running” ,“Running”}
if($services_html -notlike "<td bgcolor"){$services_html = $services_html -replace "" ,""}

but I really need to do the conditional formating only the 3rd column.

thanks for your help

Firstly, this code is not required:

$props = @{
'Service' = $service.Caption;
'StartMode' = $service.StartMode;
'StateMode' = $service.State }

$obj = New-Object -TypeName psobject -Property $props
write-output $obj

Most Powershell cmdlets, including Get-WMIObject return a PSObject. Select(-Object) basically generates a new PSObject with only the properties you define:

$services = Get-WmiObject -Computer $Computername Win32_Service -Credential $cred -Filter "Name = 'tlmagent' or Name = 'OpswareAgent'  | 
Select Caption, StartMode, State

Custom objects are typically used if you are getting data from multiple places and trying to make a single object return. Just keep that in mind before you do the extra work of creating a custom object when you can leverage Select(-Object) to do it for you.

As far as the question at hand, you are if you want to have custom HTML style based on logic, you are moving beyond pre-canned cmd-lets like ConvertTo-HTML and ConvertTo-EnhancedHTMLFragment. You have two basic choices:

  1. Build the table yourself:
    $services = Get-Service
    $header = foreach ($service in $services | select -First 1) {
        foreach ($prop in $service.PSObject.Properties) {
            "{0}" -f $prop.Name
    $tbody = foreach ($service in $services) {
        if ($service.Status -eq 'Running') {
            "{0}{1}{2}" -f $service.Status, $service.Name, $service.DisplayName
        else {
            "{0}{1}{2}" -f $service.Status, $service.Name, $service.DisplayName
    $table_frag = "{0}{1}" -f ($header -join ""), ($tbody -join "")
  2. Parse and alter the html, this method is with XML (Note: This isn't my code, it's something someone posted like 5 years ago and I couldn't find the source. Just trying to give credit where it is due):
    Add-Type -AssemblyName System.Xml.Linq

    Get the running processes to x(ht)ml

    $xml = [System.Xml.Linq.XDocument]::Parse( “$(Get-Process | ConvertTo-Html)” )

    Find the index of the column you want to format:

    $wsIndex = (($xml.Descendants(“{XHTML namespace}th”) | Where-Object { $_.Value -eq “WS” }).NodesBeforeSelf() | Measure-Object).Count

    Format the column based on whatever rules you have:

    switch($xml.Descendants(“{XHTML namespace}td”) | Where { ($.NodesBeforeSelf() | Measure).Count -eq $wsIndex } ) {
    {200MB -lt $
    .Value } { $.SetAttributeValue( “style”, “background: red;”); continue }
    {20MB -lt $
    .Value } { $.SetAttributeValue( “style”, “background: orange;”); continue }
    {10MB -lt $
    .Value } { $_.SetAttributeValue( “style”, “background: yellow;”); continue }

    Save the html out to a file


    Open the thing in your browser to see what we’ve wrought

    ii .\procs2.html

thanks for your answer. I’ll follow your advise

do you know a place where to learn more about the xml convertion.systax and more specifically how to use

Add-Type -AssemblyName System.Xml.Linq