# PowerShell Script Error Handling Strategy

**URL:** <https://forums.powershell.org/t/powershell-script-error-handling-strategy/24883>\
**Category:** PowerShell Help\
**Created:** [October 16, 2024, 2:55pm UTC](https://forums.powershell.org/t/powershell-script-error-handling-strategy/24883 "2024-10-16T14:55:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![FizzySoda](https://avatars.discourse-cdn.com/v4/letter/f/aeb1de/32.png) [@FizzySoda](https://forums.powershell.org/u/FizzySoda)\
**Post date:** [October 16, 2024, 2:55pm UTC](https://forums.powershell.org/t/powershell-script-error-handling-strategy/24883/1 "2024-10-16T14:55:42Z")

</div>

Hey All, I"m just looking for some tips on best strategy for implementing error handling on a script that contains a few functions, which are called many times.

One of the functions is executed within a Foreach Loop, so it could execute many times (50 - 200). Anyway, I have error handling and alerting (fire off an email) embedded in the functions.

My first test of the script resulted in every execution of the function in the foreach loop firing off an email. That’s a problem.

As part of the error handling, I have each function writing their errors to an error log. I really just need a single email to notify me if the script failed or succeeded. I don’t need every error emailed as well.

What’s the best way to achieve this?

---

<div class="post-metadata">

**Author:** ![neemobeer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/neemobeer/32/4087_2.png) [@neemobeer](https://forums.powershell.org/u/neemobeer)\
**Post date:** [October 16, 2024, 3:04pm UTC](https://forums.powershell.org/t/powershell-script-error-handling-strategy/24883/2 "2024-10-16T15:04:30Z")

</div>

I would say “it depends”. If each iteration of the loop is not depended on another iteration and you only want one failure notification I would just collect the error and the execution in some type of collection and a boolean indicating at least 1 failure occurred. Then after the loop send the notification of all failures.

Now if there are multiple steps in the loop that need to complete for a single execution (iteration) you could capture that with a try/catch/finally store the failure as mentioned before and continue to the next iteration. You can also try to correct the error in the catch and do some clean up in the finally block if needed

---

<div class="post-metadata">

**Author:** ![grey0ut](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/grey0ut/32/5310_2.png) [@grey0ut](https://forums.powershell.org/u/grey0ut)\
**Post date:** [October 16, 2024, 3:30pm UTC](https://forums.powershell.org/t/powershell-script-error-handling-strategy/24883/3 "2024-10-16T15:30:21Z")

</div>

i’m with @neemobeer on this. Based on your description @FizzySoda I would be inclined to capture all the errors in a variable during loop execution, then afterwards **if** that variable exists send an email with that information concatenated.

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [October 16, 2024, 5:39pm UTC](https://forums.powershell.org/t/powershell-script-error-handling-strategy/24883/4 "2024-10-16T17:39:05Z")

</div>

An easy way to collect errors from different cmdlets is to use the `-ErrorVariable` parameter

```powershell
Do-Something -ErrorVariable +errors

```

The + sign appends the errors, without it the variable would be overwritten with each command. Both the `-OutVariable` and `-ErrorVariable` parameters output `[ArrayList]` type collections.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [November 15, 2024, 5:39pm UTC](https://forums.powershell.org/t/powershell-script-error-handling-strategy/24883/5 "2024-11-15T17:39:43Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
