![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | Write-Host question Below is my test.ps1 script When I run it, PowerShell nicely 'groups' the results without repeating the headers. Name Condition ---- --------- name1 True name2 False name3 True What I'd like to do is when the 'Conditon -eq False' turn that line red. Do I need to build up the results in an array, print out the headings, loop through it and do a 'write-host -f red' ? Or is there a PowerShell shortcut that I can ? <script> function New-Item { "" | Select Name, Condition } function New-Result ($name, $condition) { $obj = New-Item $obj.Name = $name $obj.Condition = $condition $obj } New-Result "name1" $true New-Result "name2" $false New-Result "name3" $true </script> |
My System Specs![]() |
| | #2 (permalink) |
| Guest | Re: Write-Host question Doug wrote: > Below is my test.ps1 script > When I run it, PowerShell nicely 'groups' the results without repeating the > headers. > > > Name Condition > ---- --------- > name1 True > name2 False > name3 True > > > What I'd like to do is when the 'Conditon -eq False' turn that line red. > > Do I need to build up the results in an array, print out the headings, loop > through it and do a > 'write-host -f red' ? > > Or is there a PowerShell shortcut that I can ? Well, this might do something like what you want: $results = (New-Result name1 $true), (New-Result name2 $false), (New-Result name3 $true) $oldColor = $host.ui.rawui.foregroundColor $results | ? { $_.Condition } $host.ui.rawui.foregroundColor = 'red' $results | ? { !$_.Condition } $host.ui.rawui.foregroundColor = $oldColor Although it may print two tables. If you want it to print only one table, you can try editing the PowerShell formatting file (or creating a new one and importing it with Update-FormatData). |
My System Specs![]() |
| | #3 (permalink) |
| Guest | Re: Write-Host question Hi, You might want to try adapting this code: $a = 1, 2, 3, 4, 5 foreach ($element in $a){ if ($element % 2){write-host $element} else {write-host $element -foregroundcolor red} } I'm not aware of a simple way to do it using format-table. Andrew Watt MVP On Sat, 9 Dec 2006 07:58:01 -0800, Doug <Doug@discussions.microsoft.com> wrote: >Below is my test.ps1 script >When I run it, PowerShell nicely 'groups' the results without repeating the >headers. > > >Name Condition >---- --------- >name1 True >name2 False >name3 True > > >What I'd like to do is when the 'Conditon -eq False' turn that line red. > >Do I need to build up the results in an array, print out the headings, loop >through it and do a >'write-host -f red' ? > >Or is there a PowerShell shortcut that I can ? > > ><script> >function New-Item >{ > "" | Select Name, Condition >} > >function New-Result ($name, $condition) >{ > $obj = New-Item > $obj.Name = $name > $obj.Condition = $condition > > $obj >} > >New-Result "name1" $true >New-Result "name2" $false >New-Result "name3" $true ></script> |
My System Specs![]() |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| When to use write-host? | how to display variable value? | PowerShell | 1 | 08-20-2008 03:27 PM |
| 1..9|%{write-host $_:443} | PowerShell | 11 | 07-21-2007 08:57 PM | |
| redirect write-host | DouglasWoods | PowerShell | 12 | 04-26-2007 05:10 PM |
| Non ASCII characters with write-host | Peter Monadjemi | PowerShell | 6 | 02-05-2007 02:02 PM |
| Why does new-item write objects to the host? | Adam Milazzo | PowerShell | 2 | 08-28-2006 03:59 PM |