Powershell 4.0 – Try/Catch Exchange 2010 Bug?

I’ve been struggling some time now on why Powershell 4.0 are unable to “Catch” terminating errors when using Exchange 2010 Cmdlets.

When using a standard “Set-Mailbox” with Erroraction set to Stop using Powershell 3.0 the terminating error is validated by the “Try/Catch”-block:

Error with Set-Mailbox

When we use Powershell 4.0 with the same “Try/Catch”-block the result will be:

The operation couldn’t be performed because object ‘NoMailbox’ couldn’t be found on ‘DC01.contoso.com’.
+ CategoryInfo          : NotSpecified: (0:Int32) [Set-Mailbox], ManagementObjectNotFoundException
+ FullyQualifiedErrorId : 7780025A,Microsoft.Exchange.Management.RecipientTasks.SetMailbox
+ PSComputerName        : EX2010.contoso.com

A workaround for this is to use the $ErrorActionPreference variable:

Error with Set-Mailbox

Found an old thread on Technet touching this subject: Technet Forum
My speculation is whether this is a bug in the remote management of PowerShell 4.0 with Exchange 2010 Cmdlets or if it’s by design.
Either way the workaround works!

I haven’t tested this with Powershell 5.0. If you have please enlighten me if it works as it should like Powershell 3.0

5 thoughts on “Powershell 4.0 – Try/Catch Exchange 2010 Bug?”

  1. Hello,

    With PS 3.0 Try / Catch is working fine with Exchange 2010 cmdlets.

    With PS 4.0 I don’t understand why but yesterday my try / catch was working fine using the $ErrorActionPreference = ‘Stop’ trick but today it doesn’t wan’t to… As if the parameter wasn’t applied. (it’s correctly set when I check the $ErrorActionPreference during the script).

    1. Hi Toma,

      Were in the script do you set $ErrorActionPreference = ‘Stop’?
      Is your current script scope correct?

      If you try $global:ErrorActionPreference = ‘Stop’, does that work?
      / Philip

      1. I was searching how to modify this variable at the higher level.

        With this modification, it seems to work correctly!

        For the record, the $ErrorActionPreference is set just befor the Try.

        Thanks a lot!

  2. Ok, I’ve more information about the error I get.
    (Windows Server 2012 R2 / Powershell 4)

    Here is the process I follow:
    – Start EMS 2010 without any profile (empty profile)
    – Move to correct folder (thanks to the empty profile)
    – Launch the script “TestTry.ps1” which only contains the above Try / Cathch workaround.

    Result is that error isn’t trapped and Exchange error message is displayed.

    But, I found that if I run “$ErrorActionPreference = ‘Stop'” before to launch the script, error is correctly managed in EMS!

    It seems that EMS doesn’t give a sh*t about $ErrorActionPreference = ‘Stop’ while it’s included in the script.

    Am I the only one?

    Sorry to use your blog as a debug center. 🙂

Leave a Comment

Your email address will not be published. Required fields are marked *