-ErrorAction argument is ignored in favor of $ ErrorActionPreference

Both, the $ErrorActionPreferencevariable and -ErrorActionthe cmdlet argument should determine the behavior for non-termination errors .

Since the variable is global and the argument is specific to calling cmdlet, I expect the argument to override the variable. In fact, I can’t find a situation where -ErrorActiondoing 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

PS - I found this question , but is more focused on the corresponding problem, rather than on priority.

+4
source share
1 answer

-ErrorAction , , . Invoke-Expression (iex) , , . /.

OP, -ErrorAction Write-Error, , , iex.

+4

Source: https://habr.com/ru/post/1627952/


All Articles