【问题标题】:Powershell - Why isn't the exception thrown by Set-CASMailbox getting caught?Powershell - 为什么 Set-CASMailbox 抛出的异常没有被捕获?
【发布时间】:2014-08-16 08:53:09
【问题描述】:

我有一个在安装了 Exchange Server 2010 控制台的 Windows 2008 R2 服务器上运行的 Powershell 脚本。脚本 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% 的,但由于错误是引用“索引为 1 的 cmdlet 扩展代理”,我运行 Get-CmdletExtensionAgent 以查看可能是什么。运行命令后,假设我没看错,它引用的是 Query Base DN Agent

>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。对此的任何帮助将不胜感激,谢谢。

【问题讨论】:

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


    【解决方案1】:

    -ErrorAction Stop 添加到Set-CASMailbox

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-04
      • 2017-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-16
      • 2012-01-22
      相关资源
      最近更新 更多