Preventing setting 'Ignore' as the $ErrorActionPreference value is poorly enforced on the command line, and not at all in scripts
See original GitHub issueRelated: #1759
The Ignore
error-action value is meant to be used only with the -ErrorAction
common parameter, not with the $ErrorActionPreference
preference variable.
An explicit check to prevent the latter was clearly implemented (as evidenced by error message The value Ignore is not supported for an ActionPreference variable. The provided value should be used only as a value for a preference parameter, and has been replaced by the default value.
), but it doesn’t / doesn’t properly take effect.
-
On the command line,
$ErrorActionPreference = 'Ignore'
is accepted at first. When the next error occurs, it is the error message about the invalid$ErrorActionPreference
value that surfaces instead of the actual error’s, and only at that point is$ErrorActionPreference
reset toContinue
. -
In a script,
$ErrorActionPreference = 'Ignore'
is accepted and takes effect. A contributing factor is that preference variables in child scopes are not type-constrained, so I presume that no validation takes place at assignment time - see #3483
Steps to reproduce
- On the command line:
# On the command line: set to invalid value, reflect it, then trigger an error, then reflect it again.
> $ErrorActionPreference = 'Ignore'; $ErrorActionPreference; 1 / 0; $ErrorActionPreference
- In a script (place code in a
*.ps1
file):
$ErrorActionPreference = 'Ignore'; $ErrorActionPreference; 1/0; $ErrorActionPreference
Expected behavior
- Both on the command line and in a script:
The value Ignore is not supported for an ActionPreference variable. The provided value should be used only as a
value for a preference parameter, and has been replaced by the default value.
...
Continue
Attempted to divide by zero.
...
Continue
Actual behavior
- On the command line:
Ignore
$ErrorActionPreference = 'Ignore'; $ErrorActionPreference; 1 / 0: The value Ignore is not supported for an ActionPreference variable. The provided value should be used
only as a value for a preference parameter, and has been replaced by the default value. For more information, see the Help topic, "about_Preference_Variables."
+ CategoryInfo : NotSpecified: (:) [], NotSupportedException
+ FullyQualifiedErrorId : System.NotSupportedException
Continue
The value was accepted at first and retained until the next error occurred. The next error’s message is mistakenly replaced with the invalid-$ErrorActionPreference-value error message, and the value is reset to Continue
at that point.
- In a script:
Ignore
Ignore
The value was accepted, stayed in effect, and suppressed the statement-terminating error.
Environment data
PowerShell Core v6.0.0-beta.4 on macOS 10.12.5
PowerShell Core v6.0.0-beta.4 on Ubuntu 16.04.2 LTS
PowerShell Core v6.0.0-beta.4 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Windows PowerShell v5.1.15063.413 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:15 (12 by maintainers)
@PowerShell/powershell-committee reviewed this and agrees that the check for
Ignore
should be removed and allow the user to set what they want. Separately, we should have a PSScriptAnalyzer rule to detect and recommend against setting EAP.To add an additional thought for the committee:
ActionPreference.Suspend
is not supported in PowerShell Core (it’s only used for workflows), and today it just adds confusion to the code with checks to ensure that value isn’t used where there doesn’t need to be any (plus some of those checks suffer the same issues identified forActionPreference.Ignore
, since they use the same logic). Do we need to keepActionPreference.Suspend
around at this point or can we just chuck it and the half-dozen checks related to it?