February 2019
Intermediate to advanced
626 pages
15h 51m
English
The different methods PowerShell exposes to terminate a script aren't entirely consistent and may lead to confused behavior.
When throw is used to raise a terminating error, it'll stop the current script and anything that called it. In the following example, child2 will never execute:
$ErrorActionPreference = 'Continue'
function caller {
child1
child2
}
function child1 {
throw 'Failed'
'child1'
}
function child2 {
'child2'
}
caller
When the ThrowTerminatingError method is used, processing within child1 stops, but the caller function continues. This is demonstrated as follows:
function caller { child1 child2 } function child1 { [CmdletBinding()] param ( ) $errorRecord = [System.Management.Automation.ErrorRecord]::new( ...Read now
Unlock full access