Script Repository


Select mailbox archive location based on user location

February 24, 2021
1213

The script enables a mailbox archive and selects a database for the archive based on the Office property of a user account. If there is more than one mailbox database specified for a certain office, a database containing the least number of mailbox archives is selected.

To enable mailbox archives with the help of the script as a part of your business rule, custom command or scheduled task use the Run a program or PowerShell script action.

Parameters:

  • $databaseLocation - Specifies a value reference for a property based on which a mailbox database is selected.
  • $defaultDatabaseName - Specifies a name of the default mailbox database. It will be used in case if a mailbox database is not specified for a user's location.
  • $databaseInfos - Specifies a map of the possible locations and the corresponding mailbox database names. A single location can have a single or multiple databases linked to it.

Edit Remove
PowerShell
$databaseLocation = "%physicalDeliveryOfficeName%" # TODO: modify me
$defaultDatabaseName = "DefaultDatabase" # TODO: modify me

$databaseInfos = @{
    "New York" = "Database1", "Database2";
    "Boston" = "Database3";
    "Brazil" = "Database4";
    "Chicago" = "Database5";
    "Houston" = "Database6";
    "Los Angeles" = "Database7";
    "Palo ALto" = "Database8";
    "Washington DC" = "Database9", "Database10";
} # TODO: modify me. Example @{<location name> = "<database name>", "<database name>"}

$domainName = $Context.GetObjectDomain("%distinguishedName%")

# Get mailbox database
if (-not([System.String]::IsNullOrEmpty($databaseLocation)) -and ($databaseInfos.ContainsKey($databaseLocation)))
{
    $databaseNames = $databaseInfos[$databaseLocation]
}
else
{
    $databaseNames = @($defaultDatabaseName)
}

# Get mailbox database paths
# Build filter to find the databases
$filter = New-Object "System.Text.StringBuilder"
[void]$filter.Append("(&(objectCategory=msExchPrivateMDB)")
if ($databaseNames.Length -eq 1)
{
    $name = $databaseNames[0]
    [void]$filter.Append("(cn=$name)")
}
else
{
    [void]$filter.Append("(|")
    foreach ($name in $databaseNames)
    {
        [void]$filter.Append("(cn=$name)")
    }
    [void]$filter.Append(")")
}
[void]$filter.Append(")")

$searcher = $Context.BindToObject("Adaxes://$domainName/RootDSE")
$searcher.SearchFilter = $filter.ToString()
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.PageSize = 500
$searcher.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
$searcher.SetPropertiesToLoad(@("msExchArchiveDatabaseBL", "distinguishedName"))

try
{
    $searchResultIterator = $searcher.ExecuteSearch()
    $searchResults = $searchResultIterator.FetchAll()
    
    if ($searchResults.Length -eq 0)
    {
        $Context.LogMessage("Could not create a mailbox archive for the user, because the following mailbox database(s) were not found: $databaseNames", "Warning")
        return
    }
    
    # Find a mailbox database with the least number of mailbox archives
    $minArchiveMailboxCount = $NULL
    $databaseDN = $NULL
    foreach ($searchResult in $searchResults)
    {
        $msExchArchiveDatabaseBL = $searchResult.Properties["msExchArchiveDatabaseBL"].Values

        if ($msExchArchiveDatabaseBL -eq $NULL)
        {
            if (($databaseDN -eq $NULL) -or ($minArchiveMailboxCount -ne $NULL))
            {
                $databaseDN = $searchResult.Properties["distinguishedName"].Value
                break
            }
        }
        
        $archiveMailboxCount = $msExchArchiveDatabaseBL.Count
        if (($minArchiveMailboxCount -eq $NULL) -or ($archiveMailboxCount -lt $minArchiveMailboxCount))
        {
            $databaseDN = $searchResult.Properties["distinguishedName"].Value
            $minArchiveMailboxCount = $archiveMailboxCount
        }
    }
    
    # Enable the Archiving Mailbox Feature
    $mailboxParams = New-Object "Softerra.Adaxes.Adsi.Exchange.AdmExchangeMailboxParameters"

    $archiveDatabase = New-Object "Softerra.Adaxes.Adsi.AdmObjectReference"
    $archiveDatabase.ObjectDN = $databaseDN
    
    $archivingFeatureEnableParams = New-Object "Softerra.Adaxes.Adsi.Exchange.AdmExchangeEnableArchiveMailboxParams"
    $archivingFeatureEnableParams.Database = $archiveDatabase

    $archivingFeature = $mailboxParams.MailboxFeatures.Create("ADM_EXCHANGE_MAILBOXFEATURETYPE_ARCHIVE")
    $archivingFeature.Enabled = $True
    $archivingFeature.EnableParameters = $archivingFeatureEnableParams
    
    $mailboxParams.MailboxFeatures.Add($archivingFeature)
    
    # Save changes
    try
    {
        $Context.TargetObject.SetMailParameters($mailboxParams, "ADM_SET_EXCHANGE_PARAMS_FLAGS_NONE")
    }
    catch
    {
        $Context.LogMessage("An error occurred when enabling the Archiving Feature. Error: " + $_.Exception.Message, "Warning")
    }
}
finally
{
    # Release resources
    $searchResultIterator.Dispose()
}

Comments ( 0 )
No results found.
Leave a comment