The script creates a mailbox for a user selecting the mailbox database depending on a property value.
Parameters:
- $alias - Specifies a template for generating the alias (e.g. %username%).
- $propertyName - Specifies the LDAP name of the property that will specify the database for the mailbox.
- $databaseInfos - Maps values of the property specified in $propertyName with the names of mailbox databases.
PowerShell
$alias = "%username%" # TODO: modify me
$propertyName = "adm-CustomAttributeText1" # TODO: modify me
$databaseInfos = @{
"Value1" = "Database1"
"Value2" = "Database2"
"Value3" = "Database3"
} # TODO: modify me
# Get property value
try
{
$value = $Context.TargetObject.Get($propertyName)
}
catch
{
return
}
# Get mailbox database
if (($databaseInfos.ContainsKey($value)))
{
$databaseName = $databaseInfos[$value]
}
else
{
$Context.LogMessage("Could not create a mailbox for the user, because the database for the value $value is not specified.", "Warning")
return
}
# Search database
$domainName = $Context.GetObjectDomain("%distinguishedName%")
$searcher = $Context.BindToObject("Adaxes://$domainName/RootDSE")
$searcher.SearchFilter = "(&(objectCategory=msExchPrivateMDB)(cn=$databaseName))"
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.SizeLimit = 1
try
{
$searchResultIterator = $searcher.ExecuteSearch()
$searchResults = $searchResultIterator.FetchAll()
if ($searchResults.Length -eq 0)
{
$Context.LogMessage("Could not create a mailbox for the user, because the following mailbox database was not found: $databaseName", "Warning")
return
}
# Create mailbox
$mailboxDatabaseDN = $searchResults[0].Properties["distinguishedName"].Value
$Context.TargetObject.CreateMailbox($alias, "Adaxes://$mailboxDatabaseDN")
}
finally
{
# Release resources
if ($searchResultIterator) { $searchResultIterator.Dispose() }
}