Powershell - 为什么Set-CASMailbox抛出的异常没有被捕获?

时间:2014-06-25 17:07:10

标签: exception powershell try-catch exchange-server-2010 throw

我有一个Powershell脚本,该脚本在安装了Exchange Server 2010控制台的Windows 2008 R2服务器上运行。脚本 pmduaactivesync.ps1 是从任务调度程序运行的,因此必须使用奇怪的命令调用它,而不是直接调用它。以下是它的运行方式:

powershell -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\v14\bin\exshell.psc1" -exec bypass -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\Bin\Exchange.ps1'; &'C:\dev\csom\pmduaactivesync.ps1'" 

该脚本作为服务帐户运行,该帐户具有在Exchange环境中进行更改所需的所有访问权限。

现在,在脚本中,我一度尝试将邮箱上的 Exchange ActiveSync 属性设置为True。这是代码的一部分

# Try setting ActiveSync to true
try {
  Set-CASMailbox -Identity $tempUsername -ActiveSyncEnabled $true 
  if ($?) {
    # Set ASChangeValue to 1 (to be used when updating SP List)
    $ASChangeValue="1"
  } else {
    throw $error[0].Exception
  }
} catch {
  Write-Host "Exception caught with 'Set-CASMailbox -Identity $tempUsername -ActiveSyncEnabled $true' command." -ForegroundColor Red
  Write-Host "Exception Type: $($_.Exception.GetType().FullName)" -ForegroundColor Red
  Write-Host "Exception Message: $($_.Exception.Message)" -ForegroundColor Red
  $ASChangeValue="8"
}

在脚本中,我还使用Start-Transcript cmdlet创建了一个脚本。

这是我的问题。 Set-CASMailbox cmdlet正在抛出错误,如下所示:

WARNING: The cmdlet extension agent with the index 1 has thrown an exception in OnComplete(). The exception is: 
System.InvalidOperationException: Operation is not valid due to the current state of the object.

   at Microsoft.Exchange.Data.Storage.ExchangePrincipal.get_ServerFullyQualifiedDomainName()

   at Microsoft.Exchange.Data.Storage.MailboxSession.Initialize(MapiStore linkedStore, LogonType logonType, 
ExchangePrincipal owner, DelegateLogonUser delegateUser, Object identity, OpenMailboxSessionFlags flags, 
GenericIdentity auxiliaryIdentity)

   at Microsoft.Exchange.Data.Storage.MailboxSession.<>c__DisplayClass12.<CreateMailboxSession>b__10(MailboxSession 
mailboxSession)

   at Microsoft.Exchange.Data.Storage.MailboxSession.InternalCreateMailboxSession(LogonType logonType, 
ExchangePrincipal owner, CultureInfo cultureInfo, String clientInfoString, IAccountingObject budget, Action`1 
initializeMailboxSession, InitializeMailboxSessionFailure initializeMailboxSessionFailure)

   at Microsoft.Exchange.Data.Storage.MailboxSession.CreateMailboxSession(LogonType logonType, ExchangePrincipal owner,
 DelegateLogonUser delegateUser, Object identity, OpenMailboxSessionFlags flags, CultureInfo cultureInfo, String 
clientInfoString, PropertyDefinition[] mailboxProperties, IList`1 foldersToInit, GenericIdentity auxiliaryIdentity, 
IAccountingObject budget)

   at Microsoft.Exchange.Data.Storage.MailboxSession.ConfigurableOpen(ExchangePrincipal mailbox, MailboxAccessInfo 
accessInfo, CultureInfo cultureInfo, String clientInfoString, LogonType logonType, PropertyDefinition[] 
mailboxProperties, InitializationFlags initFlags, IList`1 foldersToInit, IAccountingObject budget)

   at Microsoft.Exchange.Data.Storage.MailboxSession.OpenAsSystemService(ExchangePrincipal mailboxOwner, CultureInfo 
cultureInfo, String clientInfoString)

   at Microsoft.Exchange.ProvisioningAgent.MailboxLoggerFactory.XsoMailer.Log(AdminLogMessageData data, 
LogMessageDelegate logMessage)

   at Microsoft.Exchange.ProvisioningAgent.AdminLogProvisioningHandler.OnComplete(Boolean succeeded, Exception e)

   at Microsoft.Exchange.Provisioning.ProvisioningLayer.OnComplete(Task task, Boolean succeeded, Exception exception)

我不是100%就此而言,但由于错误引用了&#34; cmdlet扩展代理的索引 1 &#34;我跑Get-CmdletExtensionAgent看看可能是什么。运行该命令后,假设我正确读取它,它将引用查询基本DN代理

>Get-CmdletExtensionAgent | Format-Table Name, Enabled, Priority
Name                                Enabled    Priority
----                                -------    --------
Admin Audit Log Agent                  True         255
Query Base DN Agent                    True           1
Rus Agent                              True           2
Mailbox Resources Management Agent     True           3
Provisioning Policy Agent              True           4
OAB Resources Management Agent         True           5
Scripting Agent                       False           6
Mailbox Creation Time Agent            True           0

这是我的大问题,

为什么不会捕获此异常?

稍后在我的脚本中,$ASChangeValue仍然设置为1,而不是8.任何对此的帮助都将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:0)

-ErrorAction Stop添加到Set-CASMailbox

Set-CASMailbox -Identity $tempUsername -ActiveSyncEnabled $true -ErrorAction Stop