The Exchange Scripting Agent is a not so well known featuere of Exchange 2010 – 2016 which offers the ability to run some own scripts or additional tasks after e.g. the creation of a mailbox.

As an example it can be used to run some more commands after the creation of a mailbox.

To enable the Scripting Agent you must do the following:

  1. Rename the ScriptingAgentConfig.xml.samle which is located in %ExchangeInstallPath%\Bin\CmdletExtensionAgents to ScriptingAgentConfig.xml
  2. Add the code you want to run
  3. Copy the XML file to all Exchange Servers in your organization
  4. Run
    Enable-CmdletExtensionAgent "Scripting Agent"

The feature is described more detailed in TechNet: https://technet.microsoft.com/en-us/library/dd297951(v=exchg.141).aspx

<?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.
	"&amp;lt;","&amp;gt;","&amp;amp;" for "less than", greater than" and "ampersand respectively.
     -->
    <Feature Name="CreateMailbox" Cmdlets="New-Mailbox">
<ApiCall Name="OnComplete">
	    if($succeeded) {
		Start-Sleep 10
		$DC = [string]($readOnlyIConfigurable.originatingserver)
		$mailbox = $ProvisioningHandler.UserSpecifiedParameters["Name"]
		if((get-mailbox $mailbox).RecipientTypeDetails -eq "SharedMailbox") {
			Set-Mailbox $mailbox -MessageCopyForSentAsEnabled $True -DomainController $DC.domain.local
			Set-Mailbox $mailbox -MessageCopyForSendOnBehalfEnabled $True -DomainController $DC.domain.local
		}
		Add-MailboxPermission $mailbox -User user -AccessRights FullAccess -AutoMapping $false
	    }
	</ApiCall>
    </Feature>
    <Feature Name="EnableMailbox" Cmdlets="Enable-Mailbox">
<ApiCall Name="OnComplete">
	    if($succeeded) {
		Start-Sleep 10
		$DC = [string]($readOnlyIConfigurable.originatingserver)
		$user = Get-User -Identity $ProvisioningHandler.UserSpecifiedParameters["Identity"]
		$mailbox = Get-Mailbox -Identity $user.DistinguishedName
		if((get-mailbox $mailbox).RecipientTypeDetails -eq "SharedMailbox") {
			Set-Mailbox $mailbox -MessageCopyForSentAsEnabled $True -DomainController $DC.domain.local
			Set-Mailbox $mailbox -MessageCopyForSendOnBehalfEnabled $True -DomainController $DC.domain.local
		}
		Add-MailboxPermission $mailbox -User user -AccessRights FullAccess -AutoMapping $false
	    }
	</ApiCall>
    </Feature>         
</Configuration>