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-Output is bleeding into a pipe why

Closed Thread
 
Thread Tools Display Modes
Old 09-24-2007   #1 (permalink)
Bob Landau
Guest
 
Posts: n/a

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
 
Old 09-25-2007   #2 (permalink)
Jeff
Guest
 
Posts: n/a

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

 
Old 09-25-2007   #3 (permalink)
Oisin Grehan
Guest
 
Posts: n/a

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


 
Old 09-25-2007   #4 (permalink)
Bob Landau
Guest
 
Posts: n/a

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
>
>
>
 
Old 09-25-2007   #5 (permalink)
Bob Landau
Guest
 
Posts: n/a

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
>
>
 
Old 09-25-2007   #6 (permalink)
Keith Hill [MVP]
Guest
 
Posts: n/a

Re: Write-Output is bleeding into a pipe why

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