在Exchange命令行管理程序中捕获错误 [英] Catching errors in the Exchange Management Shell

查看:78
本文介绍了在Exchange命令行管理程序中捕获错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写创建Exchange邮箱的Powershell脚本.只要邮箱不存在,此方法就可以正常工作,但是当我尝试捕获任何错误并将其报告回去时,脚本就好像一切正​​常一样都在运行.

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.

我发现了这个问题,它解决了为什么",我认为Enable-Mailbox命令只会引发非终止错误.

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

无论如何,所有建议的解决这些错误的方法都将失败.该cmdlet似乎忽略了$ ErrorActionPreference变量$?.始终为$ true,无论是否发生错误. $ error始终包含某些内容,因此这里也没有要检查的内容.

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"

它可以与其他所有功能一起使用,只是导致问题的Exchange命令行管理程序. Exchange环境是一台Exchange 2010服务器.

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

有什么方法可以检查cmdlet中的错误吗?

Is there any way to check the cmdlets for errors?

推荐答案

陷印错误仅适用于终止错误,看起来您从Enable-Mailbox获得的错误不是终止错误.您可以通过将ErrorAction变量的值设置为'Stop'来强制该错误为终止错误.您也可以使用try/catch(在PowerShell 2.0中)代替trap:

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" 

这篇关于在Exchange命令行管理程序中捕获错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆