Script Repository


Generate unique primary SMTP address

April 08, 2016
2203

By default, if a primary SMTP address generated for a mailbox based on Email Address Policies is not unique, Exchange resolves the ambiguity by appending a digit to the end of the local part. For example, if the address generated for a user is jdoe@example.com, and it is not unique, the ambiguity will be resolved by changing it to jdoe1@example.com , jdoe2@example.com... etc.

If such a mechanism does not meet your company standards, you can use the below script that will change the automatically generated primary SMTP address. The local part of the address will consist of X characters of the first name of a user and the user's full name, where X initially equals 1 and will be incremented by 1 until a unique SMTP address is received. Thus, for example, if an address is generated for user John Doe, and the domain part of the email address is example.com, first, the script will attempt to assign primary address jdoe@example.com. If this address is not unique, the script will attempt to assign address jodoe@example.com, then johdoe@example.com, and then johndoe@example.com.

To use the script with Adaxes, you can add it to your Business Rules, Custom Commands or Scheduled Tasks. For example, you can use it in the Business Rule that creates mailboxes for new users. The script needs to be executed after a mailbox is created. To add the script to your rule, command or task, use the Run a program or PowerShell script action.

Edit Remove
PowerShell
function IsAddressUnique($value)
{
    # Search all users who have the specified e-mail address
    $searcher = New-Object "Softerra.Adaxes.Adsi.Search.DirectorySearcher" $NULL, $False
    $searcher.SearchParameters.PageSize = 500
    $searcher.SearchParameters.SearchScope = "ADS_SCOPE_SUBTREE"
    $searcher.SearchParameters.Filter = "(&(sAMAccountType=805306368)(proxyaddresses=smtp:$value))"
    $searcher.SearchParameters.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
    $searcher.VirtualRoot = $True

    try
    {
        $searchResultIterator = $searcher.ExecuteSearch()
        $searchResults = $searchResultIterator.FetchAll()
        
       
        if($searchResults.Length -ne 0)
        {
            return $False
        }
        
        return $True
    }
    finally
    {
        # Release resources
        $searchResultIterator.Dispose()
    }
}

function GetPropertyValue($property)
{
    try
    {
        $value = $Context.TargetObject.Get($property)
    }
    catch
    {
        return $NULL
    }
    
    return $value
}

# Get first and last names
$firstName = GetPropertyValue "givenName"
$lastName = GetPropertyValue "sn"

if ([System.String]::IsNullOrEmpty($firstName) -or [System.String]::IsNullOrEmpty($lastName))
{
    $Context.LogMessage("The first and/or last name is not specified", "Warning")
    return
}


# Get E-mail address
$mail = GetPropertyValue "mail"
if ([System.String]::IsNullOrEmpty($mail))
{
    return # User doesn't have an email address
}

# Build regexp
$regexp = '^' + $firstName[0] + $lastName + '[0-9]+@+.+$'

# Check E-mail address
if ($mail -notmatch $regexp)
{
    return # The email address wasn't generated by appending a digit
}

# Get mail domain
$mailDomainPart = $mail.SubString($mail.IndexOf("@"))

# Generate new email address
$uniqueMail = $NULL
for ($i = 0; $i -lt $firstName.length; $i++)
{
    $initial = $initial + $firstName[$i]
    $uniqueMail = $initial + $lastName + $mailDomainPart
    
    if ((IsAddressUnique $uniqueMail "samAccountName"))
    {
        break
    }
    
    $uniqueMail = $NULL
}

if ([System.String]::IsNullOrEmpty($uniqueMail))
{
    $Context.LogMessage("Cannot generate unique E-mail address", "Warning")
    return
}

# Create an instance of the AdmExchangeMailboxParameters class
$mailboxParams = New-Object "Softerra.Adaxes.Adsi.Exchange.AdmExchangeMailboxParameters"

# Automatically update e-mail addresses based on e-mail address policy
$mailboxParams.EmailAddressPolicyEnabled = $False

$emailAddresses = $mailboxParams.EmailAddresses
$emailAddresses.OverrideOldValues = $False

# Add the new e-mail address to the existing list
$emailAddressToAdd = $emailAddresses.CreateAddress("ADM_EXCHANGE_ADDRTYPE_SMTP", $null)
$emailAddressToAdd.Address = $uniqueMail
$emailAddressToAdd.IsPrimary = $True
$emailAddresses.Add("ADS_PROPERTY_APPEND", $emailAddressToAdd)

# Mark old e-mail address to remove
$emailAddressToRemove = $emailAddresses.CreateAddress("ADM_EXCHANGE_ADDRTYPE_SMTP", $null)
$emailAddressToRemove.Address = $mail
$emailAddresses.Add("ADS_PROPERTY_DELETE", $emailAddressToRemove)

$mailboxParams.EmailAddresses = $emailAddresses

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

Comments ( 0 )
No results found.
Leave a comment