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