Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Write-Output is bleeding into a pipe why

Reply
 
Old 09-25-2007   #1 (permalink)
Bob Landau


 
 

Write-Output is bleeding into a pipe why

Below is a greatly simplified example of what I'm experiencing. Each time
Write-Output is being called it is being piped into my array.

Can anyone explain to me whether its me or PS?

thx
bob
==============
function Parse_Arguments( [object[]] $myargs )
{

echo "in $($MyInvocation.MyCommand.name)"
$i=0
foreach ($arg in $myargs) {echo "`$args[$i] = $($arg)"; $i++}

$Param = $myargs |
foreach {
switch ($_)
{
1 { $e = @{}; $e.a = 1; $e}
}
}

echo "`$Param.count is $($Param.Count)"

foreach ($p in $Param) { echo "`$p is $($p['a'])" }

echo "leaving $($MyInvocation.MyCommand.name)"
$Param
}



$Params = Parse_Arguments $args

## Unless the below statement is uncommented the "echo" statements above
never are written to screen
## $Params

foreach ($p in $Params)
{
## Uncomment the below statement to see what the output of Param[0] -
Param[4] is
## For some reason Write-Output is being piped into Param
$host.EnterNestedPrompt()

switch ($p.a)
{
1 { echo "1" }

Default {echo "Why is the output of `"echo`" being piped into
here?: $([int][char]$_)"}
}
}

To duplicate this write this out as t.ps1 and invoke it as .\t.ps1 1

My System SpecsSystem Spec
Old 09-25-2007   #2 (permalink)
Jeff


 
 

Re: Write-Output is bleeding into a pipe why

On Sep 25, 11:32 am, Bob Landau <BobLan...@xxxxxx>
wrote:
Quote:

> Below is a greatly simplified example of what I'm experiencing. Each time
> Write-Output is being called it is being piped into my array.
>
> Can anyone explain to me whether its me or PS?
>
> thx
> bob
> ==============
> function Parse_Arguments( [object[]] $myargs )
> {
>
> echo "in $($MyInvocation.MyCommand.name)"
> $i=0
> foreach ($arg in $myargs) {echo "`$args[$i] = $($arg)"; $i++}
>
> $Param = $myargs |
> foreach {
> switch ($_)
> {
> 1 { $e = @{}; $e.a = 1; $e}
> }
> }
>
> echo "`$Param.count is $($Param.Count)"
>
> foreach ($p in $Param) { echo "`$p is $($p['a'])" }
>
> echo "leaving $($MyInvocation.MyCommand.name)"
> $Param
>
> }
>
> $Params = Parse_Arguments $args
>
> ## Unless the below statement is uncommented the "echo" statements above
> never are written to screen
> ## $Params
>
> foreach ($p in $Params)
> {
> ## Uncomment the below statement to see what the output of Param[0] -
> Param[4] is
> ## For some reason Write-Output is being piped into Param
> $host.EnterNestedPrompt()
>
> switch ($p.a)
> {
> 1 { echo "1" }
>
> Default {echo "Why is the output of `"echo`" being piped into
> here?: $([int][char]$_)"}
> }
> }
>
> To duplicate this write this out as t.ps1 and invoke it as .\t.ps1 1
If you look at the help for Write-Output (Get-Help Write-Output), you
will see that its purpose is to write objects to the pipeline.
Anything in a function that isn't consumed by something else becomes
an output, which is why your Write-Output calls are being combined
with $Params. If you want something to go to the console, and not the
pipeline, use Write-Host.

Jeff

My System SpecsSystem Spec
Old 09-25-2007   #3 (permalink)
Oisin Grehan


 
 

Re: Write-Output is bleeding into a pipe why

On Sep 25, 3:36 am, Jeff <jeff.hill...@xxxxxx> wrote:
Quote:

> On Sep 25, 11:32 am, Bob Landau <BobLan...@xxxxxx>
> wrote:
>
>
>
>
>
Quote:

> > Below is a greatly simplified example of what I'm experiencing. Each time
> > Write-Output is being called it is being piped into my array.
>
Quote:

> > Can anyone explain to me whether its me or PS?
>
Quote:

> > thx
> > bob
> > ==============
> > function Parse_Arguments( [object[]] $myargs )
> > {
>
Quote:

> > echo "in $($MyInvocation.MyCommand.name)"
> > $i=0
> > foreach ($arg in $myargs) {echo "`$args[$i] = $($arg)"; $i++}
>
Quote:

> > $Param = $myargs |
> > foreach {
> > switch ($_)
> > {
> > 1 { $e = @{}; $e.a = 1; $e}
> > }
> > }
>
Quote:

> > echo "`$Param.count is $($Param.Count)"
>
Quote:

> > foreach ($p in $Param) { echo "`$p is $($p['a'])" }
>
Quote:

> > echo "leaving $($MyInvocation.MyCommand.name)"
> > $Param
>
Quote:

> > }
>
Quote:

> > $Params = Parse_Arguments $args
>
Quote:

> > ## Unless the below statement is uncommented the "echo" statements above
> > never are written to screen
> > ## $Params
>
Quote:

> > foreach ($p in $Params)
> > {
> > ## Uncomment the below statement to see what the output of Param[0] -
> > Param[4] is
> > ## For some reason Write-Output is being piped into Param
> > $host.EnterNestedPrompt()
>
Quote:

> > switch ($p.a)
> > {
> > 1 { echo "1" }
>
Quote:

> > Default {echo "Why is the output of `"echo`" being piped into
> > here?: $([int][char]$_)"}
> > }
> > }
>
Quote:

> > To duplicate this write this out as t.ps1 and invoke it as .\t.ps1 1
>
> If you look at the help for Write-Output (Get-Help Write-Output), you
> will see that its purpose is to write objects to the pipeline.
> Anything in a function that isn't consumed by something else becomes
> an output, which is why your Write-Output calls are being combined
> with $Params. If you want something to go to the console, and not the
> pipeline, use Write-Host.
>
> Jeff- Hide quoted text -
>
> - Show quoted text -
....and on that note, is it just me, or did anyone else expect "echo"
to be aliased to write-host, not write-output like it is now? that
surprised me.

- Oisin


My System SpecsSystem Spec
Old 09-25-2007   #4 (permalink)
Bob Landau


 
 

Re: Write-Output is bleeding into a pipe why

I'm not sure about other OS's but "echo" has always written to stdout in
Windows. However the behavior I'm seeing does surprise me.

"Oisin Grehan" wrote:
Quote:

> On Sep 25, 3:36 am, Jeff <jeff.hill...@xxxxxx> wrote:
Quote:

> > On Sep 25, 11:32 am, Bob Landau <BobLan...@xxxxxx>
> > wrote:
> >
> >
> >
> >
> >
Quote:

> > > Below is a greatly simplified example of what I'm experiencing. Each time
> > > Write-Output is being called it is being piped into my array.
> >
Quote:

> > > Can anyone explain to me whether its me or PS?
> >
Quote:

> > > thx
> > > bob
> > > ==============
> > > function Parse_Arguments( [object[]] $myargs )
> > > {
> >
Quote:

> > > echo "in $($MyInvocation.MyCommand.name)"
> > > $i=0
> > > foreach ($arg in $myargs) {echo "`$args[$i] = $($arg)"; $i++}
> >
Quote:

> > > $Param = $myargs |
> > > foreach {
> > > switch ($_)
> > > {
> > > 1 { $e = @{}; $e.a = 1; $e}
> > > }
> > > }
> >
Quote:

> > > echo "`$Param.count is $($Param.Count)"
> >
Quote:

> > > foreach ($p in $Param) { echo "`$p is $($p['a'])" }
> >
Quote:

> > > echo "leaving $($MyInvocation.MyCommand.name)"
> > > $Param
> >
Quote:

> > > }
> >
Quote:

> > > $Params = Parse_Arguments $args
> >
Quote:

> > > ## Unless the below statement is uncommented the "echo" statements above
> > > never are written to screen
> > > ## $Params
> >
Quote:

> > > foreach ($p in $Params)
> > > {
> > > ## Uncomment the below statement to see what the output of Param[0] -
> > > Param[4] is
> > > ## For some reason Write-Output is being piped into Param
> > > $host.EnterNestedPrompt()
> >
Quote:

> > > switch ($p.a)
> > > {
> > > 1 { echo "1" }
> >
Quote:

> > > Default {echo "Why is the output of `"echo`" being piped into
> > > here?: $([int][char]$_)"}
> > > }
> > > }
> >
Quote:

> > > To duplicate this write this out as t.ps1 and invoke it as .\t.ps1 1
> >
> > If you look at the help for Write-Output (Get-Help Write-Output), you
> > will see that its purpose is to write objects to the pipeline.
> > Anything in a function that isn't consumed by something else becomes
> > an output, which is why your Write-Output calls are being combined
> > with $Params. If you want something to go to the console, and not the
> > pipeline, use Write-Host.
> >
> > Jeff- Hide quoted text -
> >
> > - Show quoted text -
>
> ....and on that note, is it just me, or did anyone else expect "echo"
> to be aliased to write-host, not write-output like it is now? that
> surprised me.
>
> - Oisin
>
>
>
My System SpecsSystem Spec
Old 09-25-2007   #5 (permalink)
Bob Landau


 
 

Re: Write-Output is bleeding into a pipe why

Thank you Jeff for the explaination.

It's not that I didn't read Write-* cmdlets as well as about_redirection; I
did.

Its just that NO place have I been able to find on the Web, PS official
doc's or my trusty "PS in Action" that the Pipe Operator will gobble up
_everything_ sent to Write-Output _within_ the entire function.

Take a look below again; the pipe operator scope in my opinion should be
piping only the output in the ForEach Scriptblock. I see no reason why it
should be so greedy as to grab everything with the scope of the function.

Does anyone else find this to be as unfriendly as I do?

What I've ended up doing is write the function in a sub-expression and
explictly pipe to Out-Host. Which basically does want you've mentioned
although it does allow me to redirect to a file if I want

bob

"Jeff" wrote:
Quote:

> On Sep 25, 11:32 am, Bob Landau <BobLan...@xxxxxx>
> wrote:
Quote:

> > Below is a greatly simplified example of what I'm experiencing. Each time
> > Write-Output is being called it is being piped into my array.
> >
> > Can anyone explain to me whether its me or PS?
> >
> > thx
> > bob
> > ==============
> > function Parse_Arguments( [object[]] $myargs )
> > {
> >
> > echo "in $($MyInvocation.MyCommand.name)"
> > $i=0
> > foreach ($arg in $myargs) {echo "`$args[$i] = $($arg)"; $i++}
> >
> > $Param = $myargs |
> > foreach {
> > switch ($_)
> > {
> > 1 { $e = @{}; $e.a = 1; $e}
> > }
> > }
> >
> > echo "`$Param.count is $($Param.Count)"
> >
> > foreach ($p in $Param) { echo "`$p is $($p['a'])" }
> >
> > echo "leaving $($MyInvocation.MyCommand.name)"
> > $Param
> >
> > }
> >
> > $Params = Parse_Arguments $args
> >
> > ## Unless the below statement is uncommented the "echo" statements above
> > never are written to screen
> > ## $Params
> >
> > foreach ($p in $Params)
> > {
> > ## Uncomment the below statement to see what the output of Param[0] -
> > Param[4] is
> > ## For some reason Write-Output is being piped into Param
> > $host.EnterNestedPrompt()
> >
> > switch ($p.a)
> > {
> > 1 { echo "1" }
> >
> > Default {echo "Why is the output of `"echo`" being piped into
> > here?: $([int][char]$_)"}
> > }
> > }
> >
> > To duplicate this write this out as t.ps1 and invoke it as .\t.ps1 1
>
> If you look at the help for Write-Output (Get-Help Write-Output), you
> will see that its purpose is to write objects to the pipeline.
> Anything in a function that isn't consumed by something else becomes
> an output, which is why your Write-Output calls are being combined
> with $Params. If you want something to go to the console, and not the
> pipeline, use Write-Host.
>
> Jeff
>
>
My System SpecsSystem Spec
Old 09-25-2007   #6 (permalink)
Keith Hill [MVP]


 
 

Re: Write-Output is bleeding into a pipe why

"Bob Landau" <BobLandau@xxxxxx> wrote in message
news39C1D3D-4538-42D6-BFE8-AD321C6E3AD1@xxxxxx
Quote:

> Below is a greatly simplified example of what I'm experiencing. Each time
> Write-Output is being called it is being piped into my array.
>
> Can anyone explain to me whether its me or PS?
This is expected. I wrote about this in a blog post title: 'Effective
PowerShell Item 7: Understanding "Output"'. Perhaps you will find it
useful:

http://keithhill.spaces.live.com/blo...3A97!811.entry

--
Keith

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Send command line via WMI - Pipe output to text file on remote mac PowerShell
Re: Send command line via WMI - Pipe output to text file on remote PowerShell
Write-Output sends $null down the pipe PowerShell
No output from write-output PowerShell
write-output array without C/R PowerShell


Vista Forums 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 Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46