Reputation: 31
I have been banging my head against the desk on this and the closest I have gotten still won't work. I am trying to get this to work.
$test = Get-Service | sort Status | format-wide -Groupby Status -Column 4
$test | % {
$line = $_.ToString() <~~ even tried Out-String
if ($_.status -eq "Running") {
write-host $line -foregroundcolor red
} elseif ($_.status -eq "Stopped") {
write-host $line -foregroundcolor yellow
} else {
write-host $line
}
}
I'm trying to keep the format that I have already but I am open to suggestions. Out-String, from what I found, will not work with format-* options. Please help.
Upvotes: 2
Views: 1328
Reputation: 47872
The Format-*
cmdlets should generally be the last command in a pipeline, and shouldn't be assigned to variables. They are intended for final output, not to be later transformed. They don't retain the original objects or their properties.
If you just removed the Format-Wide
call, it would write the services with the colors:
$test = Get-Service | sort Status
$test | % {
$line = $_.ToString()
if ($_.status -eq "Running") {
write-host $line -foregroundcolor red
} elseif ($_.status -eq "Stopped") {
write-host $line -foregroundcolor yellow
} else {
write-host $line
}
}
Upvotes: 2