Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Pipeline question

188 views
Skip to first unread message

stej

unread,
Jan 15, 2010, 3:31:27 AM1/15/10
to
Hey guys, I'm a little bit confused with this oneliner:

gi c:\temp\csproj\test.csproj | % { (gc $_.fullname) -replace
'HAHAHA','abc' | sc $_.fullname }

I used it to replace some text in file. However after second look I
don't know how it is possible that it works -- the problem is in the
last cmdlet:

How is it possible that $_ has property FullName? I thought that the %
(foreach-object) takes its pipeline input, modifies it (so from object
sent by gi creates array of lines produced by -replace operator) and
sends as input to set-content. So pipeline input for sc should be
array of strings - content from the file. There should be no fullName
property at all.

It seems that the $_ variable points to some 'outer pipeline scope'.

Even
Trace-Command -Name ParameterBinding -pshost -exp { gi c:\temp\csproj
\test.csproj | % { (gc $_.fullname) -replace 'HAHAHA','abc' | sc
$_.fullname } }
doesn't help me.
There is
DEBUG: ParameterBinding Information: 0 : BIND arg [C:\temp\csproj
\test.csproj] to parameter [Path]
but I don't know where it takes the value C:\temp\csproj\test.csproj

Vadims Podans [MVP]

unread,
Jan 15, 2010, 7:08:05 AM1/15/10
to
in your pipeline is test.csproj file object, therefore this object contains
FullName property. Pipeline does not modify object, just passes them from
one command to another.

GI (Get-Item) just takes file object and do not transform this object to
anything. Just test this:

gi c:\temp\csproj\test.csproj | fl *

you will see file object and it will sent to pipeline.
--
WBR, Vadims Podans
MVP: PowerShell
PowerShell blog - www.sysadmins.lv

"stej" <cerna...@seznam.cz> rakstija zinojuma
"news:7be641f0-67ba-40f0...@r5g2000yqb.googlegroups.com"...

stej

unread,
Jan 15, 2010, 7:23:52 AM1/15/10
to
Vadim, I know that gi returns object, but then there is other other
(nested) pipeline in the foreach-object.
I would expect that when I pipe into sc, the current (inner-nested)
pipeline variable is only available, not the outer (that points to
foreach-object)
I hope I made my confusion a little mit more clear.

(Unfortunatelly I can not currently create some Posh tests.]

Vadims Podans [MVP]

unread,
Jan 15, 2010, 8:52:35 AM1/15/10
to
oh, sorry, I missed this point.

Nested pipeline don't replace $_ variable, because this variable is outside
of pipeline scriptoblock. See these examples:


gi c:\temp\csproj\test.csproj | % { (gc $_.fullname) -replace
'HAHAHA','abc' | sc $_.fullname }

and:


gi c:\temp\csproj\test.csproj | % { (gc $_.fullname) -replace
'HAHAHA','abc' | sc {$_.fullname} }

in second example I put $_.fullname in scriptoblock. In this case
set-content will try to find FullName property from -replace operator
output. In your case $_ variable is not set in scriptoblock.


--
WBR, Vadims Podans
MVP: PowerShell

PowerShell blog - www.sysadmins.lv

"stej" <cerna...@seznam.cz> rakstija zinojuma
"news:e3995b3d-4d67-4395...@r24g2000yqd.googlegroups.com"...

stej

unread,
Jan 15, 2010, 5:11:27 PM1/15/10
to
So, you say that this is because I haven't wrapped it as scriptblock.
And that scriptblocks change the scope of the pipeline (?).

Do you have any resource for this topic?

On Jan 15, 2:52 pm, "Vadims Podans [MVP]" <vpodans> wrote:
> oh, sorry, I missed this point.
>
> Nested pipeline don't replace $_ variable, because this variable is outside
> of pipeline scriptoblock. See these examples:
> gi c:\temp\csproj\test.csproj | % { (gc $_.fullname) -replace
> 'HAHAHA','abc' | sc $_.fullname }
>
> and:
> gi c:\temp\csproj\test.csproj | % { (gc $_.fullname) -replace
> 'HAHAHA','abc' | sc {$_.fullname} }
>
> in second example I put $_.fullname in scriptoblock. In this case
> set-content will try to find FullName property from -replace operator
> output. In your case $_ variable is not set in scriptoblock.
> --
> WBR, Vadims Podans
> MVP: PowerShell
> PowerShell blog -www.sysadmins.lv
>

> "stej" <cerna.ze...@seznam.cz> rakstija zinojuma

PaulChavez

unread,
Jan 15, 2010, 10:27:01 PM1/15/10
to
The input to Set-Content *is* a bunch of strings, but the positional path
parameter to set content, with the value $_.fullname, is still in scope of
the foreach scriptblock and so references the FileInfo object from your
initial get-childitem command.

-Paul

stej

unread,
Jan 16, 2010, 6:25:07 PM1/16/10
to
Thank you for your responses. I summed it up here:
http://www.leporelo.eu/blog.aspx?id=learned-again-something-about-powershell-pipelines

On Jan 16, 4:27 am, PaulChavez <PaulCha...@discussions.microsoft.com>
wrote:

Joe Morris

unread,
Jan 17, 2010, 8:05:20 AM1/17/10
to
"stej" <cerna...@seznam.cz> wrote:
>PaulChavez wrote:

>> The input to Set-Content *is* a bunch of strings, but the positional path
>> parameter to set content, with the value $_.fullname, is still in scope
>> of
>> the foreach scriptblock and so references the FileInfo object from your
>> initial get-childitem command.

>Thank you for your responses. I summed it up here:
>http://www.leporelo.eu/blog.aspx?id=learned-again-something-about-powershell-pipelines

To paraphrase your web page's opening line, I think we're all learning
something from the postings here, and not just in this thread. A tip o' the
hat (am I dating myself?) to the entire newsgroup and especially to the
PowerShell gurus who take the time to provide mini-tutorials to the rest of
us.

Joe Morris


Vadims Podans [MVP]

unread,
Jan 17, 2010, 11:37:58 AM1/17/10
to
yes.

--
WBR, Vadims Podans
MVP: PowerShell
PowerShell blog - www.sysadmins.lv

"stej" <cerna...@seznam.cz> rakstija zinojuma
"news:595b316d-3ae5-4716...@u6g2000yqf.googlegroups.com"...

0 new messages