How do I escape an inescapable error?

Welcome Forums General PowerShell Q&A How do I escape an inescapable error?

Viewing 4 reply threads
  • Author
    Posts
    • #258242
      Participant
      Topics: 13
      Replies: 40
      Points: 277
      Helping Hand
      Rank: Contributor

      Some cmdlets give error that aren’t cought by Try/Catch blocks and/or don’t respect -ErrorAction Stop/Continue.
      Two examples that spring to mind are Get-AzStorageContainer, Add-PnPListItem and Get-SwisData from the Az.Storage, SharePointPnPPowerShellOnline and SwisPowerShell modules respectively.

      So my question is, how do I catch errors from cmdlets that doesn’t respect Try/Catch blocks, and thus terminate my scripts unintentionally?

    • #258323
      Senior Moderator
      Topics: 10
      Replies: 184
      Points: 980
      Helping Hand
      Rank: Major Contributor

      Do they return any error message, or any kind of output, or does your script just crash out?
      Can you reproduce the failure conditions intentionally and reliably?
      What do you see if you execute them with Set-PSDebug -Trace 2?

    • #258335
      Participant
      Topics: 13
      Replies: 40
      Points: 277
      Helping Hand
      Rank: Contributor

      Take a look at the Free Resources > Ebooks for The Big Book of PowerShell Error Handling. Using a try\catch with ErrorAction Stop catches terminating errors. I’ve had issues when you are in a remote session that you have to globally turn erroraction to stop, the command erroraction did not work.

      Thanks Rob, but if you had read my post, you would have seen that I am using Try/Catch blocks.
      And why would anyone not have their $erroractionpreeeference set to stop?

    • #258341
      Participant
      Topics: 13
      Replies: 40
      Points: 277
      Helping Hand
      Rank: Contributor

      Do they return any error message, or any kind of output, or does your script just crash out?

      Can you reproduce the failure conditions intentionally and reliably?

      Yes, Get-AzStorageContainer and Get-SwisData reliably bombs out, and give their own, I assume, internal error messages. Add-PnPListItem sometimes does the same, and sometimes errors and continues, without respecting any error action preference.

      I will try the trace, although I doubt it will help me in catching the errors(?)…

    • #258371
      Participant
      Topics: 16
      Replies: 1794
      Points: 3,300
      Helping Hand
      Rank: Community Hero

      And why would anyone not have their $erroractionpreeeference set to stop?

      Well, the default in Powershell is Continue, not Stop:

Viewing 4 reply threads
  • You must be logged in to reply to this topic.