Cmdlet Extension Agents Part 1: Automatic archive creation
Almost a year ago, I posted an article in which I tried to show the power of Cmdlet Extension Agents in Exchange 2010, or more specifically, the Scripting Agent. Unfortunately, the Cmdlet Extension Agents are often overlooked or ignored, despite customers having requirements to customize things immediately after creating a mailbox. Therefor, I decided to write another article on this topic, hoping people take up using Scripting Agents.
Now while you can also put all sorts of post-configuration tasks in provisioning scripts, using the Scripting Agent when possible has a big bonus, because those additional actions not only run when you run the cmdlet directly from the Exchange Management Shell but also when you run them indirectly by using the Exchange Management Console.
So, as this follow up of the previous article, in which I explained what the CmdLet Extension Agents are and how to utilize the Scripting Agent to automate tasks, I’ll show you another example of a Scripting Agent and quickly walk you through it, so you can experiment with it (first in a lab of course) and tune it to your own requirements.
In this example, we’ll disable ActiveSync and configure SingleItemRecovery when creating a new user with a mailbox or mailbox-enabling an existing user. Therefor, the cmdlets we’re going to customize are New-Mailbox and Enable-Mailbox.
Open up Notepad and create a file \bin\CmdletExtensionAgents\ScriptingAgentConfig.xml located in Env:ExchangeInstallPath, e.g. C:\Program Files\Microsoft\Exchange Server\V14\Bin\CmdletExtensionAgents, using the following contents:
Note: If you’ve already got a ScriptingAgentConfig.xml file, you need to integrate the following content.
<?xml version="1.0" encoding="utf-8" ?> <Configuration version="1.0"> <Feature Name="Mailboxes" Cmdlets="New-Mailbox,Enable-Mailbox"> <ApiCall Name="OnComplete"> if($succeeded) { $Name= $provisioningHandler.UserSpecifiedParameters["Name"] Set-Mailbox $Name -SingleItemRecoveryEnabled $true Set-CASMailbox $Name -ActiveSyncEnabled $false } </ApiCall> </Feature> </Configuration>
As you can see, you’re not limited to 1 action or related cmdlets (*-Mailbox). A small explanation:
- The Cmdlets specified in this feature extension dictates which cmdlets will be extended, in this case New-Mailbox and Enable-Mailbox;
- OnComplete dictates that our script will fire when the cmdlet has finished;
- We check for OnComplete parameter $succeeded, only configuring the mailbox when the preceding events were successful;
- $provisioningHandler.UserSpecifiedParameters contains user provided parameters passed to the cmdlet. So, $provisioningHandler.UserSpecifiedParameters[“Name”] will return the value of –Name;
- We set SingleItemRecovery to $true for the mailbox specified by $Name;
- We disable ActiveSync client access for this mailbox as well.
As mentioned in part 1, distribute this XML file to all your Exchange servers in the local CmdletExtensionAgents folder. When you haven’t already enabled the Scripting Agent, do so by running the following cmdlet:
Enable-CmdletExtensionAgent “Scripting Agent”
Now, when we create a new mailbox or mailbox-enable an existing user:
.. you’ll see the SingleItemRecovery has been enabled and ActiveSync has been disabled for this mailbox by the scripting agent:
I recommend you start checking out the Scripting Agent if you haven’t already done so. You can use these examples as a starting point and work from there. More information on the Scripting Agent, alternative APIs etc. can be found here.