I'm trying to colour the lines green, yellow and red within powershell depending on the latency in milliseconds. Our remote workflow dictates that anything over 40ms is bad, so I want this to be visually represented to people who run the test, which will be wrapped as exe for them to download and run if they need.
- 1-19ms = Green
- 20-39ms = Yellow
- 40-500ms = Red
- "request timed out" = Red
However, when I try to put this down in ISE it seems to not be behaving as expected. It often returns just one solid colour for all lines and sometimes it completely misses out some lines in the traceroute test. I've deliberately chosen a random Australian IP address to run the test on as it's very far away and should give a good mix of low to high latency as the hops go on and on.
Can anyone suggest where I may have gone wrong?
$traceroute = tracert 47.74.90.56
foreach ($trace in $traceroute)
{
if ($trace -Match '[1-9] ms' )
{
write-host "$trace" -ForegroundColor green
}
elseif ($trace -Match 'request timed out' )
{
write-host "$trace" -ForegroundColor red
}
elseif ($trace -Match '[1-0][1-9] ms' )
{
write-host "$trace" -ForegroundColor green
}
elseif ($trace -Match '[2-3][0-9] ms' )
{
write-host "$trace" -ForegroundColor yellow
}
elseif ($trace -Match '[4-9][0-9] ms' )
{
write-host "$trace" -ForegroundColor red
}
elseif ($trace -Match '[1-9][0-9][0-9] ms' )
{
write-host "$trace" -ForegroundColor red
}
}