question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Preventing setting 'Ignore' as the $ErrorActionPreference value is poorly enforced on the command line, and not at all in scripts

See original GitHub issue

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 $ErrorActionPreference value that surfaces instead of the actual error’s, and only at that point is $ErrorActionPreference reset to Continue.

  • 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:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:15 (12 by maintainers)

github_iconTop GitHub Comments

3reactions
SteveL-MSFTcommented, May 8, 2019

@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.

3reactions
KirkMunrocommented, Apr 27, 2019

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 for ActionPreference.Ignore, since they use the same logic). Do we need to keep ActionPreference.Suspend around at this point or can we just chuck it and the half-dozen checks related to it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

about Preference Variables - PowerShell
Ignore : Suppresses the error message and continues to execute the command. The Ignore value is intended for per-command use, not for use...
Read more >
Force ErrorActionPreference when running PowerShell?
Ordinarily, I can change this behaviour by setting $ErrorActionPreference = Stop . I can't see a corresponding command line switch for ...
Read more >
Hey, Scripting Guy! How Can I Use $ErrorActionPreference ...
If the value of $ErrorActionPreference is set to Stop, a script will halt execution at the failed command–-even if the subsequent commands ......
Read more >
Change the Default Error Action to Improve Your PowerShell ...
The Practical Value of Stopping on Errors. I have four primary reasons for making this change in all scripts that I write: Predictability,...
Read more >
17.4. Using the $ErrorActionPreference variable
The $ErrorActionPreference variable specifies the action to take in response to an error occurring. The following values are supported: SilentlyContinue — Don't ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found