Scripting agent complaining about unexplainable null reference
Hi All,
Issue
in order to simplify mailbox enabling by Help Desk guys we decided to use cmdlet extension option with scripting Agent to achieve Archive mailbox enabling on the right Database.
The issue is that as long as we do that action for one mailbox everything is ok. As long as you select several users, the first one runs OK the others fail with the following message:
Warning:
The cmdlet extension agent with the index 5 has thrown an exception in OnComplete(). The exception is: System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.Exchange.Configuration.MonadDataProvider.MonadCommand.set_CommandType(CommandType value)
at Microsoft.Exchange.ProvisioningAgent.ScriptingAgentHandler.OnComplete(Boolean succeeded, Exception e)
at Microsoft.Exchange.Provisioning.ProvisioningLayer.OnComplete(Task task, Boolean succeeded, Exception exception)
Principle
The idea was to let the scripting agent be triggered each time enable-mailbox cmdlet is called and enable the archive mailboxe, placing it on preselected database.
Configuration
we have 4 nodes DAG, 2 are for mailbox Database (std-mbx-01 to std-mbx-10) the remaining 2 are for Archive DB (arc-mbx-01 to arc-mbx-10)
Debug Tracing
for Debug Purposes, a Log File on a share is used to log some traces. Here is a sampole log file:
03/28/2011 13:01:06 : WGE1IN007P::Scripting Agent::OnComplete---------->
03/28/2011 13:01:06 : WGE1IN007P::Scripting Agent::Parameters
Name : Identity
Value : tcsgroup.ch/Users/Outlook07
03/28/2011 13:01:06 : WGE1IN007P::Scripting Agent::MailboxPostEnablingConfiguration running for tcsgroup.ch/Users/Outlook07
03/28/2011 13:01:06 : WGE1IN007P::Scripting Agent::DCCheck for WGE3DC001P
03/28/2011 13:01:06 : WGE1IN007P::Scripting Agent::DCCheck for WGE1DC001P
03/28/2011 13:01:06 : WGE1IN007P::Scripting Agent::DCCheck for WGE3DC003P
03/28/2011 13:01:06 : WGE1IN007P::Scripting Agent::Selected DC: WGE3DC003P
03/28/2011 13:01:06 : WGE1IN007P::Scripting Agent::Processing Mailbox Outlook07 Stored on STD-MBX-08
03/28/2011 13:01:06 : WGE1IN007P::Scripting Agent::Archive DB: ARC-MBX-08
03/28/2011 13:01:06 : WGE1IN007P::Scripting Agent::OnComplete---------->
03/28/2011 13:01:06 : WGE1IN007P::Scripting Agent::Parameters
Name : Archive
Value : True
Name : ArchiveDatabase
Value : ARC-MBX-08
Name : DomainController
Value : WGE3DC003P
Name : Identity
Value : tcsgroup.ch/Users/Outlook07
03/28/2011 13:01:06 : WGE1IN007P::Scripting Agent::OnComplete Called with -Archive Parameter. exiting
03/28/2011 13:01:06 : WGE1IN007P::Scripting Agent::Setting Quota
03/28/2011 13:01:06 : WGE1IN007P::Scripting Agent::Completed ///////////////////
Source Code
<?xml version="1.0" encoding="utf-8" ?>
<Configuration version="1.0">
<!--
In order to enable Scripting Agent:
- rename this file to ScriptingAgentConfig.xml
- edit it appropriately
- run the task: enable-CmdletExtensionAgent "Scripting Agent"
In order to include into your scriptlet characters prohibited in XML, use escape sequences, e.g.
"<",">","&" for "less than", greater than" and "ampersand respectively.
-->
<Feature Name="MailboxPostEnablingConfiguration" Cmdlets="enable-mailbox">
<ApiCall Name="OnComplete">
#parameter list:
#param([ProvisioningHandler]$provisioningHandler, [bool]$succeeded, [Exception]$exception)
if ($succeeded) {
$identity = $provisioningHandler.UserSpecifiedParameters["Identity"].tostring()
$computer = get-content env:computername
$logfile = '\\wge1in007p\log\'+ (get-date -Format 'yyyy-MM-dd_hh-mm-ss') + '_' +$computer + '.log'
"$(Get-Date) : $computer::Scripting Agent::OnComplete---------->" | Out-File -FilePath $logfile -Encoding default -Append
"$(Get-Date) : $computer::Scripting Agent::Parameters" |fl | Out-File -FilePath $logfile -Encoding default -Append
$provisioningHandler.UserSpecifiedParameters |fl | Out-File -FilePath $logfile -Encoding default -Append
# enabling archive will recursively trigger the scripting Agent so we must check for this
# Also if mailbox enabling is done while specifying -archive we must not try to enable it
if ( $provisioningHandler.UserSpecifiedParameters["Archive"] -eq $True){
"$(Get-Date) : $computer::Scripting Agent::OnComplete Called with -Archive Parameter. exiting " | Out-File -FilePath $logfile -Encoding default -append
} else {
"$(Get-Date) : $computer::Scripting Agent::MailboxPostEnablingConfiguration running for $identity" | Out-File -FilePath $logfile -Encoding default -append
# Set exit condition while looking for the DC that has been used to enable the mailbox
$mbx = $null
#cycle through DCs
$DClist = Get-DomainController -DomainName 'tcsgroup.ch' | where {$_.adsite -eq 'tcsgroup.ch/Configuration/Sites/HQ-Production'}
foreach ($DC in $DCList)
{
"$(Get-Date) : $computer::Scripting Agent::DCCheck for " + $dc.name | Out-File -FilePath $logfile -Encoding default -append
$mbx = Get-user $identity -DomainController $dc.name
# this is the DC on which mailbox enabling has been run or a DC that has replicated
if ($mbx.RecipientType -eq 'UserMailbox')
{
$dcName = $dc.name
$mbx = Get-Mailbox $identity -DomainController $dcName
break
}
}
if ($mbx -ne $null)
{
"$(Get-Date) : $computer::Scripting Agent::Selected DC: $DCName" | Out-File -FilePath $logfile -Encoding default -Append
"$(Get-Date) : $computer::Scripting Agent::Processing Mailbox " + $MBX.DisplayName + " Stored on " + $MBX.database.name | Out-File -FilePath $logfile -Encoding default -Append
# we only take care of Mailboxes that are created on databases named XYZ-MBX-nn
if (($mbx.ArchiveName.Count -eq 0) -and ($MBX.database.name.substring(3,5) -eq '-MBX-'))
{
$DBIndex = $MBX.database.name.substring($MBX.database.name.length - 2, 2)
$ARCDB = "ARC-MBX-" + $DBIndex
"$(Get-Date) : $computer::Scripting Agent::Archive DB: $ARCDB" | Out-File -FilePath $logfile -Encoding default -Append
Enable-Mailbox $identity -Archive -ArchiveDatabase $ARCDB -DomainController $DCName
"$(Get-Date) : $computer::Scripting Agent::Setting Quota" | Out-File -FilePath $logfile -Encoding default -Append
set-mailbox $identity -ArchiveQuota 2GB -ArchiveWarningQuota 1.8GB -DomainController $DCName
"$(Get-Date) : $computer::Scripting Agent::Completed ///////////////////" | Out-File -FilePath $logfile -Encoding default -Append
}
}
}
}
</ApiCall>
</Feature>
<Common>
</Common>
</Configuration>
Any help will be apreciated.
Regards
March 28th, 2011 2:15pm