When I want to stop a Powershell-Script when it runs into an error, how should I stop the script?
-ErrorAction -Stop ..
try {
foo.bar
catch{
$ErrorMsg = "An error occured during foo.bar"
Write-Error -Message $ErrorMsg -ErrorAction Stop
}
or
try {
foo.bar
catch{
$ErrorMsg = "An error occured during foo.bar"
Write-Error -Message $ErrorMsg
exit
}
What is Best Practise and recommened?