Script problem with Cmdlet Extension Agent

So, I found a relatively straight forward article on how to automatically enable auditing on all new mailboxes created using the scripting agent here:

http://exchangeserverpro.com/automatically-enabled-mailbox-audit-logging-exchange-server/

I have my XML files in place on all Exchange servers, and I've enabled the scripting agent. Now when I attempt to create a new mailbox, it does create it but I get an error when it tries to enable the auditing. The error:

WARNING: The cmdlet extension agent with the index 5 has thrown an exception in OnComplete(). The exception is:
Microsoft.Exchange.Provisioning.ProvisioningException: ScriptingAgent: Exception thrown while invoking scriptlet for
OnComplete API: Cannot convert 'System.Object[]' to the type
'Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter' required by parameter 'Identity'. Specified method is not
supported.. ---> System.Management.Automation.ParameterBindingException: Cannot convert 'System.Object[]' to the type
'Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter' required by parameter 'Identity'. Specified method is not
supported. ---> System.NotSupportedException: Specified method is not supported.


I've played around with the script from the original configuration in the article as you can see here in my XML:

<?xml version="1.0" encoding="utf-8" ?>
<Configuration version="1.0">
	<Feature Name="MailboxProvisioning" Cmdlets="New-Mailbox,Enable-Mailbox">
	<ApiCall Name="OnComplete">
	if($provisioningHandler.UserSpecifiedParameters["Identity"] -ne $null)
	{
		$ID = $provisioningHandler.UserSpecifiedParameters["Identity"]
		Set-Mailbox -Identity $ID -AuditEnabled:$true
	}
	</ApiCall>
	</Feature>
</Configuration>

But to no avail. I always get the same error. I am working with Exchange 2013 SP1.

Any thoughts?

July 9th, 2015 3:10pm

Hi Matt,

You seem to have changed this line

$provisioningHandler.UserSpecifiedParameters["Identity"]

Have you tested with "Name" or "Alias"

<ApiCall Name="OnComplete">

$user = Get-User -Identity $provisioningHandler.UserSpecifiedParameters["Identity"]

	if($user -ne $null)
	{
		$ID = Get-Mailbox -Identity $user.DistinguishedName
		Set-Mailbox -Identity $ID -AuditEnabled:$true
	}
	</ApiCall>


The Identity parameter is a collection of values from other parameters. Hence you might not be able to call or list it out explicitly to a variable.

A Primer on the Exchange Management Shell:

https://technet.microsoft.com/en-us/library/bb245704%28v=exchg.80%29.aspx

Similar issue:

https://social.technet.microsoft.com/Forums/exchange/en-US/4bbcd86f-d3fa-44ea-8279-f44f8d750ca5/scripting-agent-p

Free Windows Admin Tool Kit Click here and download it now
July 13th, 2015 8:54am

Hi Matt,

New-Mailbox doesnt seem to have a '-Identity' Parameter.

gcm New-Mailbox | fl

This one works.

<?xml version="1.0" encoding="utf-8" ?>
<Configuration version="1.0">
  <Feature Name="Auditing" Cmdlets="new-mailbox">
		<ApiCall Name="OnComplete">
			#parameter list:
			#param([ProvisioningHandler]$provisioningHandler, [bool]$succeeded, [Exception]$exception)

			if($succeeded) 
			{
			$user = Get-User -Identity $provisioningHandler.UserSpecifiedParameters["Name"]

	if($user -ne $null)
	{
		$ID = Get-Mailbox -Identity $user.DistinguishedName
		Set-Mailbox -Identity $ID -AuditEnabled:$true
	}

			}
		</ApiCall>
	</Feature>
</Configuration>

-------------------------------------------------------------------------------------------------

You might comeup with this error if your scripting agent contacts a different DC where the new user is yet to replicate.

" API: The operation couldn't be performed because object 'Username' couldn't be found on DC2.contoso.com"

To avoid this you might want to hardcode the DC server -parameter on both commands.

Read this section "Dealing With Multiple Domain Controllers" in below article for more.

Exchange Scripting Agent - The Power Of Script:

http://blogs.technet.com/b/rmilne/archive/2014/06/24/exchange-scripting-agent-_2d00_-the-power-of-script.as

July 14th, 2015 1:23am