0

I have script that creates user accounts and establishes an e-mail address for those accounts. All 'create-commands' are surrounded by a try/catch block to catch errors and add a message to the output log. This works fine... except for the enable-mailbox command

try { 
    Enable-Maibox (.. parameters ...) 
}
catch {
    $errorsEncountered = $true
    Write-Output "Error establishing e-mail address for $($UserData.username)"
}

when the enable-mailbox command fails... the catch-part is skipped. Why is this? And how can I fix this?

Walter81
  • 493
  • 2
  • 8
  • 21

2 Answers2

3

Non-termineting errors are not catched. Use '-ErrorAction Stop' to make the errors terminating errors.

Enable-Maibox (.. parameters ...) -ErrorAction Stop
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • but won't that stop the script entirely? I don't want it to stop, only to report the error and carry on with the next instruction. – Walter81 Dec 20 '12 at 13:25
  • No, it will not stop your script but enter your catch as desired. – Remko Dec 20 '12 at 13:53
0

I could be wrong but "Enable-Maibox" looks mis-spelled.

  • 1
    The accepted answer seems to have answered the question, despite the misspelling. Perhaps the code was typed in by hand. In any case, this was answered several years ago. – rajah9 Mar 25 '15 at 21:15