-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Description
Related: #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$ErrorActionPreferencevalue that surfaces instead of the actual error's, and only at that point is$ErrorActionPreferencereset 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 Explicit creation of preference variables in descendant scopes should be appropriately type-constrained, as in the global scope / according to the definition of the preference variable #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
*.ps1file):
$ErrorActionPreference = 'Ignore'; $ErrorActionPreference; 1/0; $ErrorActionPreferenceExpected 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)