1

I have a simple array, $Results. It has two columns: Task, Result. I output it to a table as follows:

$(foreach($ht in $Results){new-object PSObject -Property $ht}) | Format-Table -AutoSize -Property Task, Result

The Result column will contain either "PASS" or "FAIL". I would like the PASS to be in green and the FAIL to be in red.

Anyone know of a simple way to achieve this?

Ideally only the PASS/FAIL is colorized, but I can live with the entire row being done if necessary.

1 Answer 1

2

While I thought about this one, I noticed something in the related questions on the side that looked similar "Color words in powershell script format-table output". I took one of the answers to that question, and hacked it a little, and came up with the following:

# Let's Build Some Sample Data To Play With...
$Results = @()
for( $j = 1; $j -le 25; ++$j ) {
    $ans = ( "PASS", "FAIL" ) | Get-Random
    $Results += [PSCustomObject]@{ Task = "Task{0:d2}" -f $j; Result = $ans }
}

# Here's The Important Part...
filter PassFail {
    # Adapted From: https://stackoverflow.com/a/7368884/3168105
    $Pattern = "PASS|FAIL"

    $split = $_ -split $Pattern
    $found = [regex]::Matches( $_, $Pattern, 'IgnoreCase' )
    for( $i = 0; $i -lt $split.Count; ++$i ) {
        Write-Host $split[$i] -NoNewLine
        # Just in case something else pops up...
        switch( $found[$i] ) {
            "Fail" { $Color = "Red" }
            "Pass" { $Color = "Green" }
            default { $Color = "White" }
        }
        Write-Host $found[$i] -NoNewline -ForegroundColor $Color
    }

    Write-Host
}

# And here we put it together...
$Results | Out-String | PassFail

This can be adopted to handle more results, it just needs additional lines added to the switch statement, and the additional results added to the $Pattern variable. I won't begin to say this is the best solution (or even a particularly good solution), but it works and seems to behave fairly well.

To use it like you're using it above, with the array of hashtables, you should be able to do something along these lines:

$(foreach($ht in $Results){new-object PSObject -Property $ht}) | Format-Table -AutoSize Task, Result | Out-String | PassFail
Sign up to request clarification or add additional context in comments.

3 Comments

That works, except that the output tables are off. The column headings should be "Task" and "Result". Everything in the left column should be a Task, and everything in the right should be a Result. So the colors are correct... it's just the placements are now wrong.
Also want to make sure I say Thanks for the hard work. It's beyond me -- I have neither the experience with PS or regex.
I think I see what's happening. I've updated my answer to include your command with the PassFail filter added. In my tests, it seemed to resolve the left/right order problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.