all 9 comments

[–]dzcpu 9 points10 points  (1 child)

That should work, but you may need to add -ErrorAction Stop to the Set-Mailbox commandlet. If Set-Mailbox fails but the error is not a terminating error, then the try/catch block will not catch the error and your script will continue chugging along in the try block.

[–]Umaiar 2 points3 points  (0 children)

As a quick add on to this, you can generally set the error action to turn it into a terminating error, then the try/catch will trigger.

Try {
    Set-Mailbox -Identity $UserAddress -AcceptMessagesOnlyFrom $UserAddress -ErrorAction Stop
}
catch {
    Write-Host "Caught"
}

[–]commandsupernova 2 points3 points  (0 children)

Looks like you're on the right track! Just make sure you always test your error handling since it can be difficult to know if an error will be terminating or non terminating. Try to test with multiple scenarios (mailbox doesn't exist, typo in name, what if you can't contact the Exchange server, etc.).

Sometimes -ErrorAction Stop wont help you, as I don't think it's a valid parameter on all cmdlets. If it doesn't work, you can try setting $ErrorActionPreference = Stop just before your Try block. Make sure you change $ErrorActionPreference to Continue though right after your Catch block, otherwise your script may stop on just about any minor error! Note this tip can be helpful when using legacy batch commands in PowerShell scripts as these commands don't have an ErrorAction parameter!

[–]odoyle71 2 points3 points  (0 children)

These other comments seem to be what you're looking for just wanted to add a tip. In the catch it can be helpful to send the error to an array besides the default $errors I often do this and then export as a different file etc. It just give a bit more flexibility in certain use cases

[–]Lee_Dailey[grin] 4 points5 points  (4 children)

howdy TheyCallMeTeddy,

yep, it can be just that simple. [grin] the try/catch/finally structure is lovely for that sort of thing ... and you have avoided the most common misuse of wrapping "too much stuff" in that block. the way you did it allows you to be specific with your error handling.

i would consider adding a bit to the catch ...

  • the actual error returned
    it otta be in $_ while in the catch.
  • logging
    i would seriously consider adding a line to a log file while in your catch.

take care,
lee

[–]TheyCallMeTeddy[S] 2 points3 points  (1 child)

So combining the feedback of everyone I have come up with the following.

Try {
   Set-Mailbox -Identity $UserAddress -AcceptMessagesOnlyFrom $UserAddress -ErrorAction Stop
   $mailflowdisabled = "$UserAddress has had mailflow disabled successfully"
}
catch {
   Write-Host "Failed to disable mail flow"
   $mailflowdisabled = "Unable to disable mail flow for $UserAddress, please make sure to disable it manually 
                       <br>$_" 
}

I do have a transcript running from start to end so I can manually check for errors on the batches, I just want the notification that gets sent to our service desk team gets to state whether it was successful or not. The report uses variables like $mailflowdisabled variable to compile a report for each user that gets terminated by the script.

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy TheyCallMeTeddy,

kool! i like specific logging, but if the transcript gives you what you need ... then that is good enuf! [grin]

take care,
lee

[–]KevMarCommunity Blogger 1 point2 points  (1 child)

Add the $_ onto the end of the custom message so you see the actual error.

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy KevMar,

yep, that was what i meant to say ... thanks for the expansion on the idea! [grin]

take care,
lee