Scripts (with CmdletBinding) and cmdlets all have a standard -ErrorAction parameter available when being invoked. Is there a way then, within your script, if indeed you script was invoked with -ErrorAction?
Reason I ask is because I want to know if the automatic variable value for $ErrorActionPreference, as far as your script is concerned, was set by -ErrorAction or if it is coming from your session level.
$ErrorActionPreferenceis a variable in the global(session) scope. If you run a script and don’t specify the-ErrorActionparameter, it inherits the value from the global scope ($global:ErrorActionPreference).If you specify
-ErrorActionparameter, the$ErrorActionPreferenceis changed for your private scope, meaning it’s stays the same through the script except while running code where you specified something else(ex. you call another script with another-ErrorActionvalue). Example to test:Test.ps1
Output:
If you wanna test if the script was called with the
-ErrorActionparamter, you could use ex.If you don’t know what scopes is, type this in a powershell console:
Get-Help about_scopes