Last active
September 9, 2017 10:33
-
-
Save turboBasic/c11ccbb6bf4d1020c07699ea21e8e42c to your computer and use it in GitHub Desktop.
[Get-ArgumentBindingExample.ps1] Template powershell function with placeholders explaining advanced argument binding which happens in Powershell. Usage: see comment inside #powershell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment