5

Both, $ErrorActionPreference variable and -ErrorAction cmdlet argument should define behavior for non-terminating errors.

Since variable is global and argument is specific to cmdlet invocation I expect that argument overrides variable. In fact, I can't find a situation, where -ErrorAction does anything at all.

$expression = 'Write-Error "non-terminating error"' # No exception $ErrorActionPreference = 'Continue' Invoke-Expression -Command $expression # Exception $ErrorActionPreference = 'Stop' Invoke-Expression -Command $expression # No exception, why? $ErrorActionPreference = 'Continue' Invoke-Expression -Command $expression -ErrorAction Stop # Exception, why? $ErrorActionPreference = 'Stop' Invoke-Expression -Command $expression -ErrorAction Continue 

P.S. - I've found this question but it is more focused on related issue rather on priority.

1 Answer 1

6

The -ErrorAction parameter applies to errors generated by the command it is applied to. In the case of Invoke-Expression (iex) that means errors encountered while preparing to invoke the passed expression or errors encountered post processing the results. It doesn't, however, apply to the expression/command itself.

In the OP if you apply -ErrorAction to the Write-Error command you will get the expected results, whether or not you use iex.

Sign up to request clarification or add additional context in comments.

1 Comment

You right, I was just confused with behavior of Invoke-Expression. I thought it would be logical if -ErrorAction would affect results of expression execution too.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.