【问题标题】:Catching errors in the Exchange Management Shell在 Exchange 命令行管理程序中捕获错误
【发布时间】:2012-01-25 05:17:09
【问题描述】:

我正在尝试编写一个用于创建 Exchange 邮箱的 powershell 脚本。只要邮箱不存在,它就可以正常工作,但是当我尝试捕获任何错误并将其报告回来时,脚本就会运行,就好像一切都很好。

我在一个已经存在的用户上运行脚本,它显示了错误,但它正常返回,就像创建邮箱一样。

我找到this question,解决了“为什么”,我猜Enable-Mailbox命令只会抛出非终止错误。

无论如何,所有用于捕获这些错误的建议解决方案都失败了。该 cmdlet 似乎忽略了 $ErrorActionPreference 变量 $?始终为 $true,无论是否发生错误。 $error 总是包含一些东西,所以这里也没有什么要检查的。

这是我正在使用的脚本代码,非常基础。

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"

它适用于其他一切,只是 Exchange 命令行管理程序会引起问题。 Exchange 环境是 Exchange 2010 服务器。

有什么方法可以检查 cmdlet 是否有错误?

【问题讨论】:

    标签: powershell exchange-server


    【解决方案1】:

    捕获错误仅适用于终止错误,看起来您从Enable-Mailbox 得到的错误不是终止错误。您可以通过向 ErrorAction 变量传递值“Stop”来强制错误成为终止错误。您也可以使用 try/catch(在 PowerShell 2.0 中)代替陷阱:

    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" 
    

    【讨论】:

    • 哇,将 ErrorAction 直接传递给 cmdlet 确实有效。这很有帮助,谢谢。
    【解决方案2】:

    到目前为止,对于 Exchange 2010 Cmdlet 的奇怪异常处理行为,我发现唯一可靠的解决方案是添加“-ErrorAction SilentlyContinue”,以抑制异常,然后寻找预期的结果。

    比如启用邮箱的时候,我寻找邮箱的存在,像这样……

    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
    

    【讨论】:

      猜你喜欢
      • 2017-09-02
      • 1970-01-01
      • 1970-01-01
      • 2020-09-10
      • 1970-01-01
      • 1970-01-01
      • 2020-11-07
      • 2018-05-08
      • 2019-12-11
      相关资源
      最近更新 更多