Skip to content

Instantly share code, notes, and snippets.

@alx9r
Last active June 26, 2019 17:19
Show Gist options
  • Save alx9r/6cee3a0561a10ae2a78d9e85d613164f to your computer and use it in GitHub Desktop.
Save alx9r/6cee3a0561a10ae2a78d9e85d613164f to your computer and use it in GitHub Desktop.
Demonstration of -Begin {} and -End {} vs. Pipeline Flow of Control of Beforehand and Afterward Utility Functions
function Beforehand {
param(
[Parameter(Mandatory,ValueFromPipeline)]
$InputObject,
[Parameter(Mandatory,Position=1)]
[scriptblock]
$ScriptBlock
)
begin { . $ScriptBlock }
process { ,$InputObject }
}
function Afterward {
param(
[Parameter(Mandatory,ValueFromPipeline)]
$InputObject,
[Parameter(Mandatory,Position=1)]
[scriptblock]
$ScriptBlock
)
process { ,$InputObject }
end { . $ScriptBlock }
}
"`n## Output from begin"
"`n### -Begin -End"
'a','b' |
% -Begin {'begin1'} -Process {"process1$_"} -End {'end1'} |
% -Begin {'begin2'} -Process {"process2$_"} -End {'end2'}
# begin2
# process2begin1
# process2process1a
# process2process1b
# process2end1
# end2
"`n### Beforehand Afterward"
'a','b' |
Beforehand {'begin1'} | % {"process1$_"} | Afterward {'end1'} |
Beforehand {'begin2'} | % {"process2$_"} | Afterward {'end2'}
# process2begin2
# process2process1begin1
# process2process1a
# process2process1b
# process2end1
# end2
"`n## No Output from begin"
"`n### -Begin -End"
'a','b' |
% -Begin {Write-Host 'begin1'} -Process {"process1$_"} -End {'end1'} |
% -Begin {Write-Host 'begin2'} -Process {"process2$_"} -End {'end2'}
# begin1
# begin2
# process2process1a
# process2process1b
# process2end1
# end2
"`n### Beforehand Afterward"
'a','b' |
Beforehand {Write-Host 'begin1'} | % {"process1$_"} | Afterward {'end1'} |
Beforehand {Write-Host 'begin2'} | % {"process2$_"} | Afterward {'end2'}
# begin1
# begin2
# process2process1a
# process2process1b
# process2end1
# end2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment