none
Data table color rows RRS feed

  • Question

  • Creating a data table, I am trying to change the color of a row based on Pass\Fail, where green is for Pass and red is for Fail. I'm getting hung up on setting the colors, here is what I have so far...

    Foreach ($Computer in $list) {
    if (Test-Connection -computername $computer.'XM_IPAddress' -Quiet -Count 2)
    {
        $results+=New-Object PSObject -Property @{
            Name = $Computer.Name
            Description = $Computer.description
            IP = $computer.XM_ipaddress
            Status= "Success"}
        $ResolvedIP = $null
    }
    else
    {
        $results+=New-Object PSObject -Property @{
                Name = $Computer.Name
                Description = $Computer.description
                IP = $computer.XM_ipaddress
                Status= "Failure"}
        $ResolvedIP = $null
    }
    }

    That will create the table, $list is an imported csv file with multiple columns\rows.. $Results holds the table.

    If the "Status" is Success, then I would like to print the row green, and vise versa for Failure.. I'm looking for something like below, I'm aware it will not work the way it's written, just trying to give a visual.

    foreach ($item in $Results.Status)
    {
        if ($item -eq 'success')
        {
            print row in green font
        }
        ELSE
        {
            print row in red font
        }
    
    }

    Thursday, September 12, 2019 5:11 PM

All replies

  • There is no way in PowerShell to directly color the output. You have two choices:

    1. Use Write-Host to output each line and add colors as needed.
    2. Use and HTML report with ConvertTo-Html and edit the HTML to add color to rows.


    \_(ツ)_/

    Thursday, September 12, 2019 5:35 PM
  • Jvr is right
    Perform the following treatments separately
    - conversion to html with coloring
    - printing the generated .html document

    There are a lot of PS Module dedicated to html

    But Ive found that : ConvertTo-HtmlConditionalFotmat

    This seems to do just what you want.

    Olivier

    Friday, September 13, 2019 6:49 AM