Reputation: 595
I have a master script master.ps1
which calls two scripts One.ps1
and Two.ps1
like:
&".\One.ps1"
&".\Two.ps1"
When the One.ps1
script has an error, the execution gets stopped without continuing the execution of Two.ps1
How to continue execution of Two.ps1
even if there is an error in One.ps1
?
Upvotes: 1
Views: 666
Reputation: 2718
@Martin is correct assuming that the success or failure of .\One.ps1
does not impact .\Two.ps1
and if you don't care about logging or otherwise dealing with the error. but if you would prefer to handle the error rather than just continue past it you could also use a Try{}Catch{} block as below to log the error (or take any other action you would like in the Catch{}
)
Try{
&".\One.ps1"
} Catch {
$error | Out-File "OneError.txt"
}
Try{
&".\Two.ps1"
} Catch {
$error | Out-File "TwoError.txt"
}
Other ways to format this but you get the idea.
Upvotes: 0
Reputation: 58931
You have to set the $ErrorActionPreference
to continue:
Determines how Windows PowerShell responds to a non-terminating
error (an error that does not stop the cmdlet processing) at the
command line or in a script, cmdlet, or provider, such as the
generated by the Write-Error cmdlet.
You can also use the ErrorAction common parameter of a cmdlet to
override the preference for a specific command.
$ErrorActionPreference = 'continue'
Note: As a best practice I would recommend to first determine the current error action preference, store it in a variable and reset it after your script:
$currentEAP = $ErrorActionPreference
$ErrorActionPreference = 'continue'
&".\One.ps1"
&".\Two.ps1"
$ErrorActionPreference = $currentEAP
Upvotes: 2