Website-Icon Granikos GmbH & Co. KG

Exchange Cmdlet Extensions

There are three different ways to configure new Exchange user mailboxes after these have been created.

The Exchange cmdlet extension is controlled by a scripting agent configuration file and an organizational setting to enable/disable the scripting agent.

Preparation

A scripting agent configuration file sample (ScriptingAgentConfig.xml.sample) is located in

The sample needs to be renamed to ScriptingAgentConfig.xml, to be picked up by the PowerShell engine.

As always, a slight reminder: Test any modification in a test environment first, before you use the extension in a production environment.

After successful testing and deployment, you need to enable the scripting agent using

Enable-CmdletExtensionAgent "Scripting Agent"

Example

Even though you can extend mostly any Exchange cmdlet, this example covers the extension of the New-Mailbox and Enable-Mailbox cmdlets in a multi-domain and multi AD site environment.

This extension disables the following CAS mailbox settings after a new mailbox has been created:

What does the example do?

If ($succeeded) {				
    if (!($provisioningHandler.UserSpecifiedParameters.Archive -eq $true)) {
    
        # delay execution for 10 seconds, adjust as needed
        Start-Sleep -s 10
    
        # validate parameters to use a not null parameter
        if ($null -ne $provisioningHandler.UserSpecifiedParameters["Identity"]) {
            $user = $provisioningHandler.UserSpecifiedParameters["Identity"].ToString()
        }
    }
    elseif ($null -ne $provisioningHandler.UserSpecifiedParameters["Name"]) {
        $user = $provisioningHandler.UserSpecifiedParameters["Name"].ToString()
    }
    else {
        $user = $provisioningHandler.UserSpecifiedParameters["Alias"].ToString()
    }
    
    # view entire forest in a multi domain environment
    Set-AdServerSettings -ViewEntireForest:$true
    
    # fetch domain controllers in AD site
    
    $server = Get-ExchangeServer $env:computername
    $DCs = Get-DomainController | Where-Object { $_.adsite -eq $server.site }

    $CasMailbox = $null
    foreach ($d in $DCs) {
        while ($null -eq $CasMailbox) {
            # find a valid domain controller having the updated user object
            $CasMailbox = Get-CASMailbox $user -DomainController $d.dnshostname -ErrorAction SilentlyContinue
            # fetch DCs FQDN

            $WriteDC = $d.DnsHostName
            break
        }
    }
    try {
        # set CAS features as needed
        Set-CasMailbox $user -ActiveSyncEnabled:$false -ImapEnabled:$false -PopEnabled:$false -MapiHttpEnabled:$false -DomainController $WriteDC -ErrorAction SilentlyContinue
    }
    catch {}
}

Notes

After adding the PowerShell code to the ScriptingAgentConfig.xml file, the file needs to be distributed across all Exchange servers. I recommend Paul Cunningham’s PowerShell script to distribute the scripting agent configuration file.

Be aware that the scripting agent Xml is being validated using a strict schema validation. The scripting agent XML is case-sensitive, as noted here.

Links

Kurz-URL | Short URL: https://granikos.eu/go/Cerc
Die mobile Version verlassen