The Community Book of Powershell Practices Master by Powershell.org - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

21. ERR-03 Avoid using flags to handle errors

Try to avoid setting flags:

““

Try {

$continue = $true

Do-Something -ErrorAction Stop

} Catch {

$continue = $false

}

if ($continue) {

Do-This

Set-That

Get-Those

}

Instead, put the entire "transaction" into the Try block:

Try {

Do-Something -ErrorAction Stop

Do-This

Set-That

Get-Those

} Catch {

Handle-Error

}

““

It’s a lot easier to follow the logic.