We use cookies to improve your experience.
By your continued use of this site you accept such use.
For more details please see our privacy policy and cookies policy.

Script repository

Mailbox de-provisioning and re-provisioning

February 18, 2021 Views: 3990

The 1st of these PowerShell scripts disconnects a user's mailbox, but stores information necessary to reconnect the mailbox in the properties of the user account to make it possible to reconnect it later. It can be used as a part of the user deprovsioning process, for example. The 2nd script re-provisions a mailbox by reconnecting it to the AD user account.

To use the scripts with Adaxes, you will need to create custom commands executed on User objects.

For more information on creating custom commands, see Create a Custom Command.

Also, you can add the script that disconnects a mailbox to a custom command that is used for user deprovisioning.

For more information, see Configure User Deprovisioning.

Disconnect Mailbox

This following script disconnect a user mailbox. Also, it saves the mailbox GUID and the DN of the mailbox store where the mailbox is located in Adaxes custom attributes.

Parameters:

  • $mbGUIDProperty - Specifies the name of the attribute that will be used to store the mailbox GUID. Use a property that can store binary values, for example, CustomAttributeBinary1.
  • $mbStorePathProperty - Specifies the name of the attribute that will be used to store the mailbox database DN. Use a property that can store text values, for example, CustomAttributeText1.
Edit Remove
PowerShell
$mbGUIDProperty = "adm-CustomAttributeBinary1" # TODO: modify me
$mbStorePathProperty = "adm-CustomAttributeText1" # TODO: modify me

# Get mailbox GUID and mailbox store DN
$exchangeGuid = $Context.TargetObject.Get("msExchMailboxGuid")
$mailboxStoreDN = $Context.TargetObject.Get("homeMDB")

# Save the GUID and the DN to Custom Attributes
$Context.TargetObject.Put($mbGUIDProperty, $exchangeGuid)
$Context.TargetObject.Put($mbStorePathProperty, $mailboxStoreDN)
$Context.TargetObject.SetInfo()

# Disconnect the mailbox
$Context.TargetObject.DeleteMailbox()

Reconnect Mailbox

This script reconnects a previously disconnected mailbox. If a user doesn't have a disconnected mailbox or if the mailbox has already been deleted, the script creates a new mailbox for the user.

Parameters:

  • $exchangeServer - Specifies the fully qualified domain name or IP address of your Exchange Server.
  • $alias - Specifies a template for an alias that will be assigned to the user after reconnecting a mailbox. You can use value references (e.g. %username%) to insert properties of the user account as a part of the alias.
  • $mbGUIDProperty - Specifies the name of the attribute that stores the disconnected mailbox GUID.
  • $mbStorePathProperty - Specifies the name of the attribute that stores the mailbox database DN.
Edit Remove
PowerShell
$exchangeServer = "exchangeserver.domain.com" # TODO: Modify me
$alias = "%username%" # TODO: modify me
$mbGUIDProperty = "adm-CustomAttributeBinary1" # TODO: modify me
$mbStorePathProperty = "adm-CustomAttributeText1" # TODO: modify me

function CreateMailbox($alias)
{
    # Select an Exchange mailbox store for the user based on Property Patterns
    $propertyPatternDNs = $Context.TargetObject.GetEx("adm-EffectivePropertyPatterns")
    foreach ($propertyPatternDN in $propertyPatternDNs)
    {
        # Bind to the Property Pattern
        $propertyPattern = $Context.BindToObjectByDN($propertyPatternDN)

        # Search an item that specifies an Exchange mailbox store
        foreach ($item in $propertyPattern.Items)
        {
            if ($item.PropertyName -ine "homeMDB")
            {
                continue
            }

            # Get the mailbox store
            $mailboxStorageDatabase = $item.GetNextMailboxStorageDatabase($Context.TargetObject)

            # Create a mailbox
            $Context.TargetObject.CreateMailbox($alias, $mailboxStorageDatabase.AdsPath)

            # Notify that the mailbox store has been used
            $item.NotifyMailBoxStorageDataBaseIsUsed($Context.TargetObject, $mailboxStorageDatabase)

            # Exit the function
            return
        }
    }
}

# Get mailbox GUID and Exchange mailbox store DN from Adaxes custom attributes
try
{
    $mailboxGuidByte = $Context.TargetObject.Get($mbGUIDProperty)
    $mailboxStoreDN = $Context.TargetObject.Get($mbStorePathProperty)
}
catch
{
    # No saved information about a disconnected mailbox, a new mailbox will be created
    CreateMailbox $alias
    return
}

try
{
    # Create a remote PowerShell session to the Exchange Server
    $session = New-PSSession -connectionURI "http://$exchangeServer/powershell" -ConfigurationName Microsoft.Exchange
    Import-PSSession -session $session -DisableNameChecking -AllowClobber

    # Search the user's disconnected mailbox
    $mailboxGuid = New-Object "System.Guid" (,$mailboxGuidByte)
    $disconnectMailbox = Get-MailboxStatistics -Database $mailboxStoreDN | Where {$_.DisconnectDate -ne $Null -and $_.Identity -eq $mailboxGuid}

    # If the disconnected mailbox was not found, create a new one
    if($disconnectMailbox -eq $NULL)
    {
        CreateMailbox $alias
        return
    }

    # Reconnect the mailbox
    Connect-Mailbox -Identity $mailboxGuid -Database $mailboxStoreDN -User "%distinguishedName%"
}
finally
{
    # Exit remove session, release resources
    Remove-PSSession -Session $session
}

Comments 0
Leave a comment
Loading...

Got questions?

Support Questions & Answers