Catching errors in the Exchange Management Shell

被刻印的时光 ゝ 提交于 2019-12-23 23:07:41

问题


I'm trying to write a powershell script that creates an Exchange Mailbox. This works fine as long as the Mailbox doesn't already exist, but when I try to catch any error and report it back the script just runs through as if everythign were fine.

I ran the script on an already existing user and it shows the error, but it returns normally as if the mailbox was created.

I found this question, which solved the "why", I guess the Enable-Mailbox command only throws non-terminating errors.

Anyway, all the suggested solutions to catch these errors fail. The cmdlet seems to ignore the $ErrorActionPreference variable, $? is always $true, regardless if an error occured or not. $error always contains something, so here as well nothing to check against.

This is the script code I'm using, very basic.

param( [string]$uid, [string]$email )
trap [Exception] { 
    "ERROR: " + $_.Exception.Message
    exit
}
Enable-Mailbox -Identity $uid -Database HaiTest-MBDataBase-01 -PrimarySmtpAddress $email
"SUCCESS: mailbox created successfully"

It works with everything else, it's just the Exchange Management Shell that causes trouble. The Exchange environment is an Exchange 2010 server.

Is there any way to check the cmdlets for errors?


回答1:


Trapping errors works for terminating errors only, looks like the error you get from Enable-Mailbox is not a terminating error. You can force the error to be a terminating error by passing the ErrorAction variable a value of 'Stop'. You can also use try/catch (in PowerShell 2.0) instead of trap:

param( [string]$uid, [string]$email )
trap { 
    "ERROR: " + $_.Exception.Message
    exit
}
Enable-Mailbox -Identity $uid -Database HaiTest-MBDataBase-01 -ErrorAction Stop -PrimarySmtpAddress $email 
"SUCCESS: mailbox created successfully" 



回答2:


So far, the only reliable solution I have found to the strange exception handling behavior of the Exchange 2010 Cmdlets is adding "-ErrorAction SilentlyContinue", to suppress the exceptions, then looking for the expected results.

For instance, when enabling a mailbox, I look for the existence of the mailbox, like this...

function Enable-MailboxSafely {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true)]
        $Identity,
        $Database,
        [switch]$WhatIf
    )

    $output = [pscustomobject][ordered]@{
        Identity = $Identity.ToString()
        Mailbox = $null
        Message = 'Unknown Error'
        Success = $false
    }

    $output.Mailbox = Get-Mailbox -Identity $Identity -ErrorAction SilentlyContinue

    if ($output.Mailbox -ne $null) {
        $output.Message = 'Mailbox already exists.'
        $output.success = $true
    }
    Else {
        $null = Enable-Mailbox -Identity $Identity -Database:$Database -ErrorAction SilentlyContinue
        # Have to look for a mailbox, as a workaround to the Exchange 2010 Cmdlets not implementing exceptions correctly.
        $output.Mailbox = Get-Mailbox -Identity $Identity -DomainController:$DomainController -ErrorAction SilentlyContinue
        if ($output.Mailbox -ne $null) {
            $output.Message = "Mailbox created for [$Identity] on database [$Database]."
            $output.success = $true
        }
        else {
            $output.Message = "Failed to create mailbox for [$Identity] on database [$Database]."
            $output.Success = $false
        }
    }
    Write-Output $output
}

Enable-MailboxSafely -Identity Somebody


来源:https://stackoverflow.com/questions/8589063/catching-errors-in-the-exchange-management-shell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!