Windows Vista Forums

Steppable Pipelines in Powershell 2.0 CTP

  1. #1


    Tao Ma Guest

    Steppable Pipelines in Powershell 2.0 CTP

    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 System SpecsSystem Spec

  2. #2


    Marco Shaw [MVP] Guest

    Re: Steppable Pipelines in Powershell 2.0 CTP

    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 System SpecsSystem Spec

  3. #3


    Marco Shaw [MVP] Guest

    Re: Steppable Pipelines in Powershell 2.0 CTP


    > 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 System SpecsSystem Spec

  4. #4


    Marco Shaw [MVP] Guest

    Re: Steppable Pipelines in Powershell 2.0 CTP

    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 System SpecsSystem Spec

  5. #5


    TaoMa Guest

    Re: Steppable Pipelines in Powershell 2.0 CTP

    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 System SpecsSystem Spec

  6. #6


    Maximilian Hnel Guest

    Re: Steppable Pipelines in Powershell 2.0 CTP

    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 System SpecsSystem Spec

  7. #7


    TaoMa Guest

    Re: Steppable Pipelines in Powershell 2.0 CTP

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

    It's all my fault.

    Best wishes!

    "Maximilian Hnel" <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 System SpecsSystem Spec


Steppable Pipelines in Powershell 2.0 CTP
Similar Threads
Thread Forum
pipelines supported by PowerShell cmdlets PowerShell
commenting multiline pipelines PowerShell
How do you comment long pipelines PowerShell
Colored Text and Pipelines PowerShell
ctrl-c affects background pipelines PowerShell