I have a Powershell script that is run on a Windows 2008 R2 server with Exchange Server 2010 console installed on it. The script, pmduaactivesync.ps1, is run from Task scheduler, so it has to be called using a weird command rather than just calling it directly. Here is how it is run:
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'"
The script is being run as a service account, and that account has all the access it needs to make changes in the Exchange environment.
Now, in the script, at one point I am trying to set the Exchange ActiveSync attribute on a mailbox to True. Here is that part of the code
# 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"
}
In the script, I'm also creating a transcript using the Start-Transcript
cmdlet.
Here is my problem. The Set-CASMailbox
cmdlet is throwing an error, shown below:
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)
I'm not 100% on this, but since the error is referencing "The cmdlet extension agent with the index 1" I ran Get-CmdletExtensionAgent
to see what that might be. After running the command, assuming I'm reading it correctly, it is referencing the 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
Here is my big question,
Why is this exception not getting caught?
Later in my script, $ASChangeValue
is still set to 1, as opposed to 8. Any help on this would be appreciated, thanks.