Function Test-PipelineAndBinding {
# Usage:
#
# . .\Test-PipelineAndBinding.ps1
# Test-Self
[CMDLETBINDING(PositionalBinding=$False)]
[OUTPUTTYPE([String[]])]
PARAM(
[PARAMETER( ValueFromPipeline, ValueFromPipelineByPropertyName )]
[ALLOWEMPTYSTRING()] [ALLOWEMPTYCOLLECTION()] [ALLOWNULL()]
[Object[]]
$pipeItem = $null,
[PARAMETER( ValueFromRemainingArguments )]
[ALLOWEMPTYSTRING()] [ALLOWEMPTYCOLLECTION()] [ALLOWNULL()]
[Object[]]
$restArgs
)
BEGIN{
# Here we have:
# $restArgs - equals to all arguments
# (not from the pipeline, but which are to the right of Cmdlet name)
Write-Host -ForegroundColor Green "BEGIN block"
Write-Host -ForegroundColor Green "input = $input"
Write-Host -ForegroundColor Green "pipeItem = $pipeItem"
Write-Host -ForegroundColor Green "_ = $_"
Write-Host -ForegroundColor Green "restArgs = $restArgs"
}
PROCESS{
# Here we have:
# $_ - single current item from the pipeline
# $pipeItem - the same as above
# $input - the same as above
#
# $restArgs is here as well - it doesn't change unless we change it ourselves here or in BEGIN{}
Write-Host -ForegroundColor Cyan " PROCESS block"
Write-Host -ForegroundColor Cyan " input = $input"
Write-Host -ForegroundColor Cyan " pipeItem = $pipeItem"
Write-Host -ForegroundColor Cyan " _ = $_"
Write-Host -ForegroundColor Cyan " restArgs = $restArgs"
}
END{
# Here we have:
# $_ - equals the last value of $_ from the PROCESS{} block
# $pipeItem - the same as $_
#
# $restArgs is here as well - it doesn't change unless we change it somewhere above
Write-Host -ForegroundColor Yellow "END block"
Write-Host -ForegroundColor Yellow "input = $input"
Write-Host -ForegroundColor Yellow "pipeItem = $pipeItem"
Write-Host -ForegroundColor Yellow "_ = $_"
Write-Host -ForegroundColor Yellow "restArgs = $restArgs"
}
}
function Test-Self {
Write-Host -ForegroundColor Red "`nTest-PipelineAndBinding"; Test-PipelineAndBinding
Write-Host -ForegroundColor Red "`nTest-PipelineAndBinding 10 20 30 40"; Test-PipelineAndBinding 10 20 30 40
Write-Host -ForegroundColor Red "`nTest-PipelineAndBinding 10,20,30,40"; Test-PipelineAndBinding 10,20,30,40
Write-Host -ForegroundColor Red "`nTest-PipelineAndBinding 10 20,30 40"; Test-PipelineAndBinding 10 20,30 40
Write-Host -ForegroundColor Red "`nTest-PipelineAndBinding 10 @(20,30) 40"; Test-PipelineAndBinding 10 @(20,30) 40
Write-Host -ForegroundColor Red "`nTest-PipelineAndBinding 10 @(20,@(21,22),30) 40"; Test-PipelineAndBinding 10 @(20,@(21,22),30) 40
Write-Host -ForegroundColor Red "`nTest-PipelineAndBinding 10,@(20,@(21,22),30),40"; Test-PipelineAndBinding 10,@(20,@(21,22),30),40
}