Skip to content

Instantly share code, notes, and snippets.

@nicholasdille
Last active February 4, 2016 20:24
Show Gist options
  • Save nicholasdille/af34b169efcd090d929c to your computer and use it in GitHub Desktop.
Save nicholasdille/af34b169efcd090d929c to your computer and use it in GitHub Desktop.
Demonstrated how a PowerShell advanced function behaves when trying to return
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