Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

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.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Write-Host question

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 12-09-2006   #1 (permalink)
Doug
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 SpecsSystem Spec
Old 12-09-2006   #2 (permalink)
Adam Milazzo
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 SpecsSystem Spec
Old 12-20-2006   #3 (permalink)
Andrew Watt [MVP]
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 SpecsSystem Spec
Closed Thread

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


Update your Vista Drivers Update Your Drivers Now!!

Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008