Automating Tasks With Scripting Agent Cmdlet Extension Agent In Exchange 2010…

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.

Get all cmdletextensionagents

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 BinCmdletExtensionAgents. 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.

Sample scripting agent config

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.



            
            if($succeeded)
            {                PowerShell cmdlets            }

       
   
    

Below is the code that I used to achieve what my client wanted.


  

   
       
   
            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
                            }

       
         

The line 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 BinCmdletExtensionAgents folder.

scripting agent config

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.

Enable scripting agent

Time to test! I created a test mailbox and the scripting agent configured all the options my client wanted.

Test Mailbox

Confirmation of working scripting agent

Now that you understand the power of scripting agent, you can automate tasks to make your life easier Winking smile

SUBSCRIBE FOR DAILY ARTICLE UPDATES VIA EMAIL
Get the published articles delivered straight to your inbox. Your details will not be passed to any third party company.

Exchange Architect, Blogger, Husband & Dad. I have been in IT for the last 11 years, with Exchange Server becoming the prime area in the last few years. I am active on TechNet forums & Experts Exchange.

View all contributions by

19 comments… add one

  • Daniel June 30, 2011 at 12:18 am

    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?

    Reply edit
  • Vojin Lekovic June 30, 2011 at 7:22 am

    I'm getting error on each command after I used exactly same code:

    WARNING: An unexpected error has occurred and a Watson dump is being generated: Object reference not set to an instance
    of an object.
    Object reference not set to an instance of an object.
    + CategoryInfo : NotSpecified: (:) [Get-CmdletExtensionAgent], NullReferenceException
    + FullyQualifiedErrorId : System.NullReferenceException,Microsoft.Exchange.Management.ProvisioningAgentTasks.GetCm
    dletExtensionAgent

    Reply edit
    • Patrick Cossette December 6, 2012 at 7:32 pm

      I got the same problem as you. I started from an empty SciptingAgentConfig.xml, then copy/pasted code from above. I then got that Watson error each time I ran a cmdlet, ANY cmdlet, not just New-Maibox. I also got the same Watson error if I copy/pasted the code from Pat Richard’s post.

      So I instead started from the ScriptingAgentConfig.xml.sample file, copy/pasted the FILE into ScriptingAgentconfig.xml (the FILE, not the contents!). I then deleted sections i did not need, and modified what I needed. No more Watson error!

      Pat

      Reply edit
  • Rajith Jose Enchiparambil June 30, 2011 at 10:02 am

    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.

    Reply edit
  • Rajith Jose Enchiparambil June 30, 2011 at 10:04 am

    Hi Vojin,

    Can you copy paste your code here?

    Reply edit
  • Deepak July 3, 2011 at 1:15 pm

    Thanks Rajith, very useful feature but not been advertised/promoted/blogged by many. Thanks for sharing.

    Reply edit
  • Rajith Jose Enchiparambil July 3, 2011 at 4:01 pm

    Thanks Deepak.

    Reply edit
  • Jakobi July 8, 2011 at 2:02 pm

    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!

    Reply edit
  • Rajith Jose Enchiparambil July 8, 2011 at 3:12 pm

    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?

    Reply edit
  • Jakobi July 11, 2011 at 8:11 am

    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!

    Reply edit
  • Rajith Jose Enchiparambil July 11, 2011 at 8:48 am

    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?

    Reply edit
  • Jakobi July 11, 2011 at 9:09 am

    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

    Reply edit
  • Jakobi July 11, 2011 at 1:51 pm

    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

    Reply edit
  • Rajith Jose Enchiparambil July 11, 2011 at 2:09 pm

    Glad to know that it is sorted out.

    Thanks Jakobi for the update.

    Reply edit
  • Jim August 4, 2012 at 1:07 am

    Rajith,

    Do I need to enable the scripting agent and copy the .xml file to ALL my Exchange servers, or just my MBX servers? I have 2 CAS servers and 4 MBX servers.

    Thanks,

    Jim

    Reply edit
  • Henrik March 23, 2013 at 10:51 am

    Hi,

    I have tried to set calendar permissions to Reviewer default on new mailboxes, but I keep getting an error.
    The script is:

    if($succeeded) {
    $newmailbox= $provisioningHandler.UserSpecifiedParameters["Name"]
    Add-MailBoxFolderPermission -Identity $newmailbox”:\Calendar” -User Default -AccessRights Reviewer
    }

    But I receive this 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: An undefined executing user property is being used. Property Name :’executingUserid’.. —> Microsoft.Exchange.Configuration.Authorization.ExecutingUserPropertyNotFoundException: An undefined executing user property is being used. Property Name :’executingUserid’.
    at Microsoft.Exchange.Management.StoreTasks.SetTenantXsoObjectWithFolderIdentityTaskBase`1.PrepareMailboxUser()
    at Microsoft.Exchange.Management.StoreTasks.SetMailboxFolderPermissionBase.CreateSession()
    at Microsoft.Exchange.Configuration.Tasks.DataAccessTask`1.InternalStateReset()
    at Microsoft.Exchange.Configuration.Tasks.SetObjectTaskBase`2.InternalStateReset()
    at Microsoft.Exchange.Configuration.Tasks.SetTenantADTaskBase`3.InternalStateReset()
    at Microsoft.Exchange.Management.StoreTasks.SetTenantXsoObjectWithFolderIdentityTaskBase`1.InternalStateReset()
    at Microsoft.Exchange.Configuration.Tasks.Task.ProcessRecord()
    at System.Management.Automation.CommandProcessor.ProcessRecord()
    — End of inner exception stack trace —
    at Microsoft.Exchange.ProvisioningAgent.ScriptingAgentHandler.OnComplete(Boolean succeeded, Exception e)
    at Microsoft.Exchange.Provisioning.ProvisioningLayer.OnComplete(Task task, Boolean succeeded, Exception exception)

    Reply edit

Speak Your Mind…

Website Hits