【发布时间】:2014-12-09 08:34:51
【问题描述】:
我正在尝试测试一个为现有用户创建交换邮箱的脚本。最后,我想要一个测试并通知用户它是否成功的小块。我所拥有的如下。
$mail = Get-Mailbox user@domain.com
$checkmail = @($mail).count
if($checkmail -eq 0)
{
write-host "Does not exist"
}
else
{
write-host "exists"
}
这实际上工作得很好,但是当对象不存在时,它也会吐出一个巨大的 Powershell 错误来引导。我只是不希望那部分在那里。我已经在整个事情上尝试了一个 try/catch 块,但由于某种原因它只是忽略了它。错误如下:
Get-Mailbox : The operation could not be performed because object 'user@domain.com' could not
be found on the domain controller 'domainnamehere'
+ $mail = Get-Mailbox user@domain.com
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-Mailbox], ManagementObjectNotFoundException
+ FullyQualifiedErrorID : 3AAE54AC,Microsoft.Exchange.Management.RecipientTasks.Getmailbox
任何帮助将不胜感激。
【问题讨论】:
-
你能告诉我们你为
try块做了什么吗?您可能需要设置-ErrorAction的Get-Mailbox
标签: powershell error-handling try-catch