SOLVED

Azure Function Apps - PowerShell's ErrorActionPreference

%3CLINGO-SUB%20id%3D%22lingo-sub-43540%22%20slang%3D%22en-US%22%3EAzure%20Function%20Apps%20-%20PowerShell's%20ErrorActionPreference%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-43540%22%20slang%3D%22en-US%22%3E%3CP%3EBy%20default%20the%20environment's%20%24ErrorActionPreference%20is%20set%20to%20'Continue'.%20When%20a%20cmdlet%20throws%20an%20error%20the%20script%20will%20continue.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20want%20it%20to%20'Stop'%20and%20get%20caught%20in%20the%20catch%20of%20my%20try-catch%20block.%20In%20my%20script%20I%20am%20able%20to%20set%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24ErrorActionPreference%20%3D%20%22Stop%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eand%20if%20I%20print%20the%20value%20to%20the%20screen%20I%20can%20see%20that%20%26nbsp%3Bit%20is%20now%20set%20to%20'Stop'%20and%20not%20'Continue'.%20However%2C%20when%20my%20cmdlet%20throws%20an%20error%20it%20still%20continues.%20It%20is%20ignoring%20my%20error%20action%20preference%20and%20behaving%20according%20to%20the%20default%20value.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECan%20anyone%20shed%20any%20light%20on%20this%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-43540%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-43732%22%20slang%3D%22en-US%22%3ERe%3A%20Azure%20Function%20Apps%20-%20PowerShell's%20ErrorActionPreference%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-43732%22%20slang%3D%22en-US%22%3E%3CP%3EI%20was%20able%20to%20get%20it%20to%20behave%20by%20using%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%3CSPAN%20class%3D%22pln%22%3E%24global%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3A%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Eerroractionpreference%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22lit%22%3E1%3C%2FSPAN%3E%3C%2FPRE%3E%3CP%3EInstead%20of%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%24ErrorActionPreference%20%3D%20'Stop'%3C%2FPRE%3E%3CP%3E%3CIMG%20id%3D%22robotlol%22%20class%3D%22emoticon%20emoticon-robotlol%22%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Fi%2Fsmilies%2F16x16_robot-lol.png%22%20alt%3D%22Robot%20LOL%22%20title%3D%22Robot%20LOL%22%20%2F%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

By default the environment's $ErrorActionPreference is set to 'Continue'. When a cmdlet throws an error the script will continue.

 

I want it to 'Stop' and get caught in the catch of my try-catch block. In my script I am able to set:

 

$ErrorActionPreference = "Stop"

 

and if I print the value to the screen I can see that  it is now set to 'Stop' and not 'Continue'. However, when my cmdlet throws an error it still continues. It is ignoring my error action preference and behaving according to the default value.

 

Can anyone shed any light on this?

1 Reply
best response confirmed by ray hogan (New Contributor)
Solution

I was able to get it to behave by using:

 

$global:erroractionpreference = 1

Instead of

 

$ErrorActionPreference = 'Stop'

Robot LOL