Cmdlet extension agents are new components in Exchange 2010 that are called by Exchange 2010 cmdlets, when they are run. It is used to extend the functionality of Exchange 2010 cmdlets by running a script or set of code after the Exchange cmdlet has run. For example, you can run a set of configurations after a new mailbox has been created, basically calling the scripting agent to run a code after the New-Mailbox cmdlet completes. Scripting agent is one of the seven cmdlet extension agents available in Exchange 2010 and is the one that is disabled by default.
Get-CmdletExtensionAgent will list all the seven agents. Run Get-CmdletExtensionAgent | ft name, priority, enabled –wrap –autosize to get a refined output.
I looked at the scripting agent as one of my client asked me ways to automate things to a certain extend. I will explain the process of using the scripting agent based on what I had been asked. My client wanted to make sure that all newly created mailboxes had the following configurations, without manually setting each.
- POP, IMAP, ActiveSync is disabled
- Outlook Anywhere is disabled
- Single Item Recovery is enabled
- Fields like Country, City, Street Address, Company in the user properties needs to be filled in automatically.
- Block users from using Outlook in online mode
- Set the company OWA Mailbox Policy
Scripting agent can be used to do what my client wanted and I set it up for them. The scripting agent, when enabled, looks for a file named “ScriptingAgentConfig.xml” within the \Bin\CmdletExtensionAgents. By default, a “ScriptingAgentConfig.xml.sample” file exists in the same location which gives an idea of the code that needs to go in the xml file. Open the sample file using notepad and understand the basic syntax the xml file needs.
Below is the basic syntax you need for the config xml file, irrespective of what you are trying to achieve. Pat Richard’s post helped me with the syntax.
<?xml version="1.0" encoding="utf-8" ?>
<Configuration version="1.0">
<Feature Name="Name" Cmdlets="cmdlets">
<ApiCall Name="OnComplete">
if($succeeded)
{ PowerShell cmdlets }
</ApiCall>
</Feature>
</Configuration>
Below is the code that I used to achieve what my client wanted.
<?xml version="1.0" encoding="utf-8" ?>
<Configuration version="1.0">
<Feature Name="MailboxProvisioning" Cmdlets="new-mailbox">
<ApiCall Name="OnComplete">
if($succeeded)
{
$mailbox = $provisioningHandler.UserSpecifiedParameters["Alias"]
Set-CASMailbox $mailbox -ImapEnabled $false -POPEnabled $false –ActiveSyncEnabled $false –OWAMailboxPolicy "HEW OWA Policy” –MAPIBlockOutlookRpcHTTP $true –MAPIBlockOutlookNonCachedMode $true
Set-User -City 'London' -CountryOrRegion 'United Kingdom' -Company 'How Exchange Works' -Identity $mailbox
Set-Mailbox $mailbox -SingleItemRecoveryEnabled $true
}
</ApiCall>
</Feature>
</Configuration>
The line <Feature Name=”MailboxProvisioning” Cmdlets=”new-mailbox,enable-mailbox”> explains that we want our code to run whenever a new-mailbox or enable-mailbox cmdlet runs successfully. Once one of the cmdlets (new-mailbox or enable-mailbox) runs successfully either via Shell, Console or custom applications, we are instructing the scripting agent to run a set of PowerShell cmdlets which all Exchange admins are familiar with. In our case, the cmdlets needed to run are Set-CASMailbox, Set-User and Set-Mailbox. We are storing the alias of the user to a variable ($mailbox) in our case to re-use in the commands below.
I copied the entire code below to notepad and saved it as ScriptingAgentConfig.xml inside \Bin\CmdletExtensionAgents folder.
Next step is to enable the scripting agent. The agent should only be enabled once the ScriptingAgentConfig.xml is in place. Otherwise, all Exchange cmdlets except the “Get-noun” will throw errors. Run Enable-CmdletExtensionAgent “Scripting Agent” to enable the agent.
Time to test! I created a test mailbox and the scripting agent configured all the options my client wanted.
Now that you understand the power of scripting agent, you can automate tasks to make your life easier
12 comments:
This is really handy. Looking forward to finishing our Exchange 2003 -> 2010 migration and putting stuff like this into use.
I'm guessing that this needs to be done on each machine that you're running EMS from, right?
If you have more than one Exchange 2010 server, then all servers need to have the same xml file. Easiest will be to copy & paste in all servers.
Hi Vojin,
Can you copy paste your code here?
Thanks Rajith, very useful feature but not been advertised/promoted/blogged by many. Thanks for sharing.
Thanks Deepak.
Hi!
I've used more or less the same code as you did, but I get an exception when creating a mailbox:
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: The operation couldn't be performed because object 'username' couldn't be found on 'dc2.domain.local'.. ---> Microsoft.Exchange.Configuration.Tasks.ManagementObjectNotFoundException: The operation couldn't be performed because object 'hlinda' couldn't be found on 'dc2.domain.local'.
The code:
if($succeeded)
{
$mailbox = $provisioningHandler.UserSpecifiedParameters["Alias"]
Set-User -City 'Stockholm' -CountryOrRegion 'Sweden' -Company 'Company Inc' -Identity $mailbox
Set-Mailbox $mailbox -SingleItemRecoveryEnabled $true
}
If I do the commands manually they work.
Oh, and thanks for an excellent blog!
Hi Jakobi,
If you have more than one server running Exchange 2010, you'll need to enable the extension agent on each, and create the .xml file.
Does this apply to you?
Hi!
Thanks for the reply, I have three servers, two MB (DAG) and one CAS/HUB.
When I check if agent is enabled with EMS on each server it is listed as enabled, but I ran cmdlet to enable it anyways.
Got the same error when creating a new user after that thou, the .xml has been placed on all three.
Thanks!
Hi Jakobi,
In your error message above """ The operation couldn't be performed because object 'hlinda' couldn't be found on 'dc2.domain.local'""""""
What is hlinda? A new mailbox user you created?
Actually that was an existing user, but I have also tried to create a new one but with the same error.
In both cases the mailbox is created, just not with the settings I put in the .xml
I found this just now: http://social.technet.microsoft.com/Forums/nl-NL/exchange2010/thread/4bbcd86f-d3fa-44ea-8279-f44f8d750ca5 and thought I might try it out.
/Jakob
Hi again Rajith!
It seems my issues stemmed from our 2003 env. that I migrated from a month ago, I removed the CN=Servers container from the old first administrative group due to public folder issues, and that seems to have solved this issue as well because when I created a new users just now everything went without a hitch!
Thanks for a great blog and because you take time to answer comments :)
/Jakob
Glad to know that it is sorted out.
Thanks Jakobi for the update.
Post a Comment