Last active
February 4, 2016 20:24
-
-
Save nicholasdille/af34b169efcd090d929c to your computer and use it in GitHub Desktop.
Demonstrated how a PowerShell advanced function behaves when trying to return
This file contains 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-ReturnFromFunction { | |
[CmdletBinding()] | |
param( | |
[switch] | |
$ReturnFromBegin | |
, | |
[switch] | |
$ReturnFromProcess | |
, | |
[switch] | |
$ReturnFromEnd | |
) | |
Begin { | |
Write-Output ('[{0}] Entering BEGIN block' -f $MyInvocation.MyCommand) | |
if ($ReturnFromBegin) { | |
Write-Output ('[{0}] Returning from BEGIN block' -f $MyInvocation.MyCommand) | |
return | |
} | |
Write-Output ('[{0}] Leaving BEGIN block' -f $MyInvocation.MyCommand) | |
} | |
Process { | |
Write-Output ('[{0}] Entering PROCESS block' -f $MyInvocation.MyCommand) | |
if ($ReturnFromProcess) { | |
Write-Output ('[{0}] Returning from PROCESS block' -f $MyInvocation.MyCommand) | |
return | |
} | |
Write-Output ('[{0}] Leaving PROCESS block' -f $MyInvocation.MyCommand) | |
} | |
End { | |
Write-Output ('[{0}] Entering END block' -f $MyInvocation.MyCommand) | |
if ($ReturnFromEnd) { | |
Write-Output ('[{0}] Returning from END block' -f $MyInvocation.MyCommand) | |
return | |
} | |
Write-Output ('[{0}] Leaving END block' -f $MyInvocation.MyCommand) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment