I expect that the return from the function statement (begin
, process
, end
) should also terminate all its statements, but this does not happen.
I have a test function like this:
function test($a) {
begin {
Write-Host 'begin ' -NoNewline
if ($a -like '*begin*') { return; }
}
process {
Write-Host 'process ' -NoNewline
if ($a -like '*process*') { return; }
}
end {
Write-Host 'end' -NoNewline
if ($a -like '*end*') { return; }
}
}
And when I run it this way:
'begin', 'process', 'end' | ForEach-Object {
Write-Host "Try exit in $_ :`t" -NoNewline
test $_
Write-Host
}
Then I get a similar result:
Try exit in begin : begin process end
Try exit in process : begin process end
Try exit in end : begin process end
But I would like the output from the function to also terminate its other statements and the output would be like this:
Try exit in begin : begin
Try exit in process : begin process
Try exit in end : begin process end
How can I get it?
I tryed to use break
, continue
and exit
instead of return
but it terminated all script..