Suppose we have a PS filter X:
filter X([Parameter(ValueFromPipeline)]$o)
{
begin { Write-Host "Begin X" }
process { "|$o|" }
end { Write-Host "End X" }
}
Here is the result of running it:
C:\> 0..3 | X
Begin X
|0|
|1|
|2|
|3|
End X
C:\>
I want to write another filter Y which must invoke X internally. The desired output of running 0..3 | Y
should be:
Begin Y
Begin X
|0|
|1|
|2|
|3|
End X
End Y
Here is my current implementation:
filter Y([Parameter(ValueFromPipeline)]$o)
{
begin { Write-Host "Begin Y" }
process { $o | X }
end { Write-Host "End Y" }
}
Unfortunately, it does not work as I need:
C:\> 0..3 | Y
Begin Y
Begin X
|0|
End X
Begin X
|1|
End X
Begin X
|2|
End X
Begin X
|3|
End X
End Y
C:\>
Which is perfectly expected, because the correct implementation should be something like this:
filter Y([Parameter(ValueFromPipeline)]$o)
{
begin { Write-Host "Begin Y" ; InvokeBeginBlockOf X }
process { $o | InvokeProcessBlockOf X }
end { Write-Host "End Y" ; InvokeEndBlockOf X }
}
This is pseudo code, of course, but the point is - I must be able to invoke the begin/process/end blocks of the X filter individually and that would be the correct way to invoke filter from a filter.
So my question is - how can we invoke filter from a filter by individually invoking the begin/process/end blocks from the respective blocks in the outer filter?