Skip to content

Instantly share code, notes, and snippets.

@iricigor
Last active November 16, 2020 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iricigor/3a34687ae90553c5aec011349a875d06 to your computer and use it in GitHub Desktop.
Save iricigor/3a34687ae90553c5aec011349a875d06 to your computer and use it in GitHub Desktop.
PowerShell pipeline argument binding
function fa {
[CmdletBinding()]
param (
[Parameter(Mandatory,ValueFromPipeline)]
[int]$x
)
# function 1
BEGIN {Write-Verbose "Begin a: $x"}
PROCESS {
Write-Verbose " Process a: $x"
# two return values
2*$x
3*$x
}
END {Write-Verbose "End a"}
}
function fb {
[CmdletBinding()]
param (
[Parameter(Mandatory,ValueFromPipeline)]
[int]$y
)
# function 1
BEGIN {Write-Verbose " Begin b: $y"}
PROCESS {
Write-Verbose " Process b: $y"
# two return values
$y*$y
$y*$y*$y
}
END {Write-Verbose " End b"}
}
2,3 | fa -Verbose | fb -Verbose
# Output:
#
# VERBOSE: Begin a: 0
# VERBOSE: Begin b: 0
# VERBOSE: Process a: 2
# VERBOSE: Process b: 4
# 16
# 64
# VERBOSE: Process b: 6
# 36
# 216
# VERBOSE: Process a: 3
# VERBOSE: Process b: 6
# 36
# 216
# VERBOSE: Process b: 9
# 81
# 729
# VERBOSE: End a
# VERBOSE: End b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment