Script Repository


Select store for mailbox archive based on office location and the least number of mailboxes

September 25, 2017
913

The script creates a mailbox archive for a user. A store where to place the archive is selected based on the user's office location and the least number of mailboxes in the store.

Store names must consist of a certain prefix and a suffix. The prefix can be any sequence of characters. The suffix must be the order number of the store in the range of stores associated with the office. Thus, for example, an office called My Office can be associated with mailbox stores whose names start with MyOffice and the range of 1 through 5. This means that if a user's office is set to My Office in AD, their mailbox archive will be created in any of mailbox stores from MyOffice01 through MyOffice05, depending on which store contains the least number of mailboxes.

Parameter:

  • $locationInfos - specifies a map of available offices, corresponding mailbox prefixes and the ranges of mailboxes associated with them.
Edit Remove
PowerShell
$locationInfos = @{
    "Washington" = "WDCStore", @(1..10)
    "London" = "LStore", @(15..25)
} # TODO: modify me. Example @{<Office> = "<Store name prefix>", "<Range of suffixes to store names>"}

function SearchObjects($path, $filter, $properties)
{
    # Set search options
    $searcher = $Context.BindToObject($path)
    $searcher.SearchFilter = $filter
    $searcher.SearchScope = "ADS_SCOPE_SUBTREE"
    $searcher.PageSize = 500
    $searcher.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
    $searcher.SetPropertiesToLoad($properties)
    
    try
    {
        # Fetch results
        $searchResultIterator = $searcher.ExecuteSearch()
        $searchResults = $searchResultIterator.FetchAll()
        
        return ,$searchResults
    }
    finally
    {
        # Release resources
        if ($searchResultIterator){ $searchResultIterator.Dispose() }
    }
}

if ($Context.TargetObject.RecipientType -ne "ADM_EXCHANGERECIPIENTTYPE_MAILBOXENABLED")
{
    return # The user doesn't have a mailbox
}

# Get mailbox stores for the user's office
if ([System.String]::IsNullOrEmpty("%physicalDeliveryOfficeName%"))
{
    $Context.LogMessage("Office is not specified for user. Archive mailbox will not be created.", "Warning")
    return
}
if (-not($locationInfos.ContainsKey("%physicalDeliveryOfficeName%")))
{
    $Context.LogMessage("There are no mailbox stores specified for office '%physicalDeliveryOfficeName%'. Archive mailbox will not be created.", "Warning")
    return
}
$storeInfo = $locationInfos["%physicalDeliveryOfficeName%"]

# Find mailbox stores for the specified office
# Build search filter
$filter = New-Object "System.Text.StringBuilder"
[void]$filter.Append("(&(objectClass=msExchPrivateMDB)(|")
$storeInfo[1] | %%{[void]$filter.Append("(name=$($storeInfo[0])$($_.ToString('00')))")}
[void]$filter.Append("))")

# Execute the search in the user's domain
$domainName = $Context.GetObjectDomain("%distinguishedName%")
$searchResults = SearchObjects "Adaxes://$domainName/RootDSE" $filter.ToString() @("homeMDBBL", "msExchArchiveDatabaseBL", "distinguishedName")
if ($searchResults.Length -eq 0)
{
    $Context.LogMessage("Could not locate any mailbox mailbox stores for office '%physicalDeliveryOfficeName%'", "Warning")
    return
}

# Get the mailbox store with the least number of mailboxes
$stores = @{}
foreach ($searchResult in $searchResults)
{
    # Get number of mailboxes in the store
    $userMailboxes = $searchResult.Properties["homeMDBBL"].Values
    $archiveMailboxes = $searchResult.Properties["msExchArchiveDatabaseBL"].Values
    $totalMailboxCount = $userMailboxes.Count + $archiveMailboxes.Count
    $stores.Add($searchResult.Properties["distinguishedName"].Value, $totalMailboxCount)
}

# Specify parameters for the Archiving Mailbox Feature 
$dbInfo = $stores.GetEnumerator() | Sort Value -Descending | Select -Last 1
$archiveDatabase = New-Object "Softerra.Adaxes.Adsi.AdmObjectReference"
$archiveDatabase.ObjectDN = $dbInfo.Key.ToString()
$archivingFeatureEnableParams = New-Object "Softerra.Adaxes.Adsi.Exchange.AdmExchangeEnableArchiveMailboxParams"
$archivingFeatureEnableParams.Database = $archiveDatabase

# Enable Archive mailbox
$mailboxParams = New-Object "Softerra.Adaxes.Adsi.Exchange.AdmExchangeMailboxParameters"
$archivingFeature = $mailboxParams.MailboxFeatures.Create("ADM_EXCHANGE_MAILBOXFEATURETYPE_ARCHIVE")
$archivingFeature.Enabled = $True
$archivingFeature.EnableParameters = $archivingFeatureEnableParams

# Add Archiving feature to the collection of mailbox features
$mailboxParams.MailboxFeatures.Add($archivingFeature)

# Save the changes
$Context.TargetObject.SetMailParameters($mailboxParams, "ADM_SET_EXCHANGE_PARAMS_FLAGS_NONE")


Comments ( 0 )
No results found.
Leave a comment