|
1 |
| -# ERR-01 Use -ErrorAction Stop when calling cmdlets |
2 |
| - |
3 |
| -When trapping an error, try to use -ErrorAction Stop on cmdlets to generate terminating, trappable exceptions. |
4 |
| - |
5 |
| -# ERR-02 Use $ErrorActionPreference='Stop' or 'Continue' when calling non-cmdlets |
6 |
| - |
7 |
| -When executing something other than a cmdlet, set $ErrorActionPreference='Stop' before executing, and re-set to Continue afterwards. If you're concerned about using -ErrorAction because it will bail on the entire pipeline, then you've probably over-constructed the pipeline. Consider using a more scripting-construct-style approach, because those approaches are inherently better for automated error handling. |
8 |
| - |
9 |
| -Ideally, whatever command or code you think might bomb should be dealing with one thing: querying one computer, deleting one file, updating one user. That way, if an error occurs, you can handle it and then get on with the next thing. |
10 |
| - |
11 |
| -# ERR-03 Avoid using flags to handle errors |
12 |
| - |
13 |
| -Try to avoid setting flags: |
14 |
| - |
15 |
| -```PowerShell |
16 |
| -try { |
17 |
| - $continue = $true |
18 |
| - Do-Something -ErrorAction Stop |
19 |
| -} catch { |
20 |
| - $continue = $false |
21 |
| -} |
22 |
| -
|
23 |
| -if ($continue) { |
24 |
| - Do-This |
25 |
| - Set-That |
26 |
| - Get-Those |
27 |
| -} |
28 |
| -``` |
29 |
| - |
30 |
| -Instead, put the entire "transaction" into the Try block: |
31 |
| - |
32 |
| -```PowerShell |
33 |
| -try { |
34 |
| - Do-Something -ErrorAction Stop |
35 |
| - Do-This |
36 |
| - Set-That |
37 |
| - Get-Those |
38 |
| -} catch { |
39 |
| - Handle-Error |
40 |
| -} |
41 |
| -``` |
42 |
| - |
43 |
| -It's a lot easier to follow the logic. |
44 |
| - |
45 |
| -# ERR-04 Avoid using $? |
46 |
| - |
47 |
| -When you need to examine the error that occurred, try to avoid using $?. It actually doesn't mean an error did or did not occur; it's reporting whether or not the last-run command considered itself to have completed successfully. You get no details on what happened. |
48 |
| - |
49 |
| - |
50 |
| -# ERR-05 Avoid testing for a null variable as an error condition |
51 |
| - |
52 |
| -Also try to avoid testing for a null variable as an error condition: |
53 |
| - |
54 |
| -``` |
55 |
| -$user = Get-ADUser -Identity DonJ |
56 |
| -
|
57 |
| -if ($user) { |
58 |
| - $user | Do-Something |
59 |
| -} else { |
60 |
| - Write-Warning "Could not get user $user" |
61 |
| -} |
62 |
| -``` |
63 |
| - |
64 |
| -There are times and technologies where that's the only approach that will work, especially if the command you're running won't produce a terminating, trappable exception. But it's a logically contorted approach, and it can make debugging trickier. |
65 |
| - |
66 |
| -# ERR-06 Copy $Error[0] to your own variable |
67 |
| - |
68 |
| -Within a `catch` block, `$_` will contain the last error that occurred, as will `$Error[0]`. Use either - but immediately copy them into your own variable, as executing additional commands can cause `$_` to get "hijacked" or `$Error[0]` to contain a different error. |
69 |
| - |
70 |
| -It isn't necessary to clear `$Error` in most cases. `$Error[0]` will be the last error, and PowerShell will maintain the rest of the `$Error` collection automatically. |
| 1 | +# ERR-01 Use -ErrorAction Stop when calling cmdlets |
| 2 | + |
| 3 | +When trapping an error, try to use -ErrorAction Stop on cmdlets to generate terminating, trappable exceptions. |
| 4 | + |
| 5 | +# ERR-02 Use $ErrorActionPreference='Stop' or 'Continue' when calling non-cmdlets |
| 6 | + |
| 7 | +When executing something other than a cmdlet, set $ErrorActionPreference='Stop' before executing, and re-set to Continue afterwards. If you're concerned about using -ErrorAction because it will bail on the entire pipeline, then you've probably over-constructed the pipeline. Consider using a more scripting-construct-style approach, because those approaches are inherently better for automated error handling. |
| 8 | + |
| 9 | +Ideally, whatever command or code you think might bomb should be dealing with one thing: querying one computer, deleting one file, updating one user. That way, if an error occurs, you can handle it and then get on with the next thing. |
| 10 | + |
| 11 | +# ERR-03 Avoid using flags to handle errors |
| 12 | + |
| 13 | +Try to avoid setting flags: |
| 14 | + |
| 15 | +```PowerShell |
| 16 | +try { |
| 17 | + $continue = $true |
| 18 | + Do-Something -ErrorAction Stop |
| 19 | +} catch { |
| 20 | + $continue = $false |
| 21 | +} |
| 22 | +
|
| 23 | +if ($continue) { |
| 24 | + Do-This |
| 25 | + Set-That |
| 26 | + Get-Those |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +Instead, put the entire "transaction" into the Try block: |
| 31 | + |
| 32 | +```PowerShell |
| 33 | +try { |
| 34 | + Do-Something -ErrorAction Stop |
| 35 | + Do-This |
| 36 | + Set-That |
| 37 | + Get-Those |
| 38 | +} catch { |
| 39 | + Handle-Error |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +It's a lot easier to follow the logic. |
| 44 | + |
| 45 | +# ERR-04 Avoid using $? |
| 46 | + |
| 47 | +When you need to examine the error that occurred, try to avoid using $?. It actually doesn't mean an error did or did not occur; it's reporting whether or not the last-run command considered itself to have completed successfully. You get no details on what happened. |
| 48 | + |
| 49 | + |
| 50 | +# ERR-05 Avoid testing for a null variable as an error condition |
| 51 | + |
| 52 | +Also try to avoid testing for a null variable as an error condition: |
| 53 | + |
| 54 | +``` |
| 55 | +$user = Get-ADUser -Identity DonJ |
| 56 | +
|
| 57 | +if ($user) { |
| 58 | + $user | Do-Something |
| 59 | +} else { |
| 60 | + Write-Warning "Could not get user $user" |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +There are times and technologies where that's the only approach that will work, especially if the command you're running won't produce a terminating, trappable exception. But it's a logically contorted approach, and it can make debugging trickier. |
| 65 | + |
| 66 | +# ERR-06 Copy $Error[0] to your own variable |
| 67 | + |
| 68 | +Within a `catch` block, `$_` will contain the last error that occurred, as will `$Error[0]`. Use either - but immediately copy them into your own variable, as executing additional commands can cause `$_` to get "hijacked" or `$Error[0]` to contain a different error. |
| 69 | + |
| 70 | +It isn't necessary to clear `$Error` in most cases. `$Error[0]` will be the last error, and PowerShell will maintain the rest of the `$Error` collection automatically. |
0 commit comments