• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Steppable Pipelines in Powershell 2.0 CTP

T

Tao Ma

#1
I notice this new feature:
Steppable Pipelines

This feature allows turning a script-block into a steppable pipeline and
then controlling the sequence of activities by individually calling Begin(),
Process() and End() on the pipeline.

I dont know what is the meaning of this. Who can offer an example for me.

Thanks in advance,

Best wishes.
 

My Computer

M

Marco Shaw [MVP]

#2
Tao Ma wrote:

> I notice this new feature:
> Steppable Pipelines
>
> This feature allows turning a script-block into a steppable pipeline and
> then controlling the sequence of activities by individually calling
> Begin(), Process() and End() on the pipeline.
Example:

PSH> function step {

>> begin{write-host "Starting..."}
>> process{$_.name}
>> end{write-host "Ending..."}
>> }
>>
PS> get-childitem . *.ps1|step
Starting...
1.ps1
.... <--removed
test.ps1
test1.ps1
.... <--removed
Ending...

In the begin() and end() part, you're not actually doing anything with
the objects coming in.

Marco


--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com
 

My Computer

M

Marco Shaw [MVP]

#3

> Example:
>
> PSH> function step {
> begin{write-host "Starting..."}
> process{$_.name}
> end{write-host "Ending..."}
> }
>
> PS> get-childitem . *.ps1|step
> Starting...
> 1.ps1
> ... <--removed
> test.ps1
> test1.ps1
> ... <--removed
> Ending...
>
> In the begin() and end() part, you're not actually doing anything with
> the objects coming in.
OK, this is the basic concept behind a steppable pipeline, but isn't
really the new feature added in the v2 CTP.

I use a function above to demonstrate, but v1 functions can also do the
above.

This may be more of an addition to the new scriptcmdlets feature, the
more I think about this.

I can't come up with a practical example right now, but will think about it.

Marco

--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com
 

My Computer

M

Marco Shaw [MVP]

#4
This is from the v2 CTP readme below. Another example can be found in
Sapien's PowerShell TFM *3rd edition* ebook:

Get-childitem ScriptCmdlet that only shows containers if ¨Ccontainersonly
parameter is passed.

cmdlet Get-ChildItem `
{
param(
[Position(0)] `
[ValueFromPipeline] `
[ValueFromPipelineByPropertyName] `
[System.String[]] `
$Path,

[switch] $ContainersOnly
)
begin
{
$wrappedCmdlet = get-command -type cmdlet Get-ChildItem
[void] $CommandLineParameters.Remove("ContainersOnly")
if ($containersonly)
{
$sb = {& $wrappedCmdlet @CommandLineParameters | `
where {$_.PSIsContainer} }
}
else
{
$sb = { & $wrappedCmdlet @CommandLineParameters }
}
$sp = $sb.GetSteppablePipeline()
$sp.Begin($cmdlet)
}
process { $sp.Process($_) }
end { $sp.End() }
}
 

My Computer

T

TaoMa

#5
Thank you for your help. It's really hard to figure out this new features.
I wrote a wired code like this:
1..3 | &{ BEGIN {"head"} PROCESS{$_} END{"tail"} }

Latterly, I found it can be processed in V1. It seems no one cares this
feature at all. Maybe this is not a big deal :)

Best wishes

"Marco Shaw [MVP]" <marco.shaw@_NO_SPAM_gmail.com> дÈëÓʼþ
news:OcrtBSFJIHA.3672@xxxxxx

>

> > Example:
> >
> > PSH> function step {
> > begin{write-host "Starting..."}
> > process{$_.name}
> > end{write-host "Ending..."}
> > }
> >
> > PS> get-childitem . *.ps1|step
> > Starting...
> > 1.ps1
> > ... <--removed
> > test.ps1
> > test1.ps1
> > ... <--removed
> > Ending...
> >
> > In the begin() and end() part, you're not actually doing anything with
> > the objects coming in.
>
> OK, this is the basic concept behind a steppable pipeline, but isn't
> really the new feature added in the v2 CTP.
>
> I use a function above to demonstrate, but v1 functions can also do the
> above.
>
> This may be more of an addition to the new scriptcmdlets feature, the
> more I think about this.
>
> I can't come up with a practical example right now, but will think about
it.

>
> Marco
>
> --
> Microsoft MVP - Windows PowerShell
> http://www.microsoft.com/mvp
>
> PowerGadgets MVP
> http://www.powergadgets.com/mvp
>
> Blog:
> http://marcoshaw.blogspot.com
 

My Computer

M

Maximilian Hänel

#6
Hi TaoMa,

> Latterly, I found it can be processed in V1. It seems no one cares this
> feature at all. Maybe this is not a big deal :)
I think the key point is that you can convert an arbitry script block to
a steppable pipline "object". With this kind of object, you can the call
the begin, process and end method _yourself_ whereas with an ordinary
scriptblock you can only call the "whole thing" at once.

Marco already posted an example:

$sb = {& $wrappedCmdlet @CommandLineParameters | `
where {$_.PSIsContainer} }
$sp = $sb.GetSteppablePipeline()

now you can call begin, process and end on the $sp variable - the
steppable pipeline.

$sp.Begin()
$sp.Process('bla') # call process as often as you want - step by step!
$sp.Process('foo')
#...
$sp.End() #end of story..

Without converting the scriptblock to a steppable pipeline you must call
it as "one big thing":

.. $sp # run the whole script/pipeline/whatever

This is only a guess, however...

cu

Max
 

My Computer

T

TaoMa

#7
I'm too careless to read Marco Shaw's post. Thank you for your help.

It's all my fault.

Best wishes!

"Maximilian Hänel" <ngSpam@xxxxxx> ????
news:%23s0bMfSJIHA.4228@xxxxxx

> Hi TaoMa,
>

> > Latterly, I found it can be processed in V1. It seems no one cares this
> > feature at all. Maybe this is not a big deal :)
>
> I think the key point is that you can convert an arbitry script block to
> a steppable pipline "object". With this kind of object, you can the call
> the begin, process and end method _yourself_ whereas with an ordinary
> scriptblock you can only call the "whole thing" at once.
>
> Marco already posted an example:
>
> $sb = {& $wrappedCmdlet @CommandLineParameters | `
> where {$_.PSIsContainer} }
> $sp = $sb.GetSteppablePipeline()
>
> now you can call begin, process and end on the $sp variable - the
> steppable pipeline.
>
> $sp.Begin()
> $sp.Process('bla') # call process as often as you want - step by step!
> $sp.Process('foo')
> #...
> $sp.End() #end of story..
>
> Without converting the scriptblock to a steppable pipeline you must call
> it as "one big thing":
>
> . $sp # run the whole script/pipeline/whatever
>
> This is only a guess, however...
>
> cu
>
> Max
 

My Computer