Hello,
Thank you for clarifying. You need to use the below script and a Scheduled Task configured for User Object type. The task needs to be executed just once.
$addressToAdd = "%firstname%.%lastname%@company.com" # TODO: modify me
function IsAddressUnique($value)
{
$searcher = $Context.BindToObject("Adaxes://rootDSE")
$searcher.SearchFilter = "(&(sAMAccountType=805306368)(proxyAddresses=smtp:$value))"
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.PageSize = 500
$searcher.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
$searcher.SetPropertiesToLoad($properties)
$searcher.SizeLimit = 1
$searcher.VirtualRoot = $True
try
{
$searchResultIterator = $searcher.ExecuteSearch()
$searchResults = $searchResultIterator.FetchAll()
return $searchResults.Length -eq 0
}
finally
{
# Release resources
if ($searchResultIterator){ $searchResultIterator.Dispose() }
}
}
function AddNewEmailAddress($addressToAdd, $mailboxParams)
{
$emailAddresses = $mailboxParams.EmailAddresses
# Create a new e-mail address
$emailAddress = $emailAddresses.CreateAddress("ADM_EXCHANGE_ADDRTYPE_SMTP", $null)
$emailAddress.Address = $addressToAdd
$emailAddress.IsPrimary = $False
# Add the new e-mail address to the existing list
$emailAddresses.Add("ADS_PROPERTY_APPEND", $emailAddress)
$emailAddresses.OverrideOldValues = $False
$mailboxParams.EmailAddresses = $emailAddresses
$Context.TargetObject.SetMailParameters($mailboxParams, "ADM_SET_EXCHANGE_PARAMS_FLAGS_NONE")
}
if ($Context.TargetObject.RecipientType -ne "ADM_EXCHANGERECIPIENTTYPE_MAILBOXENABLED")
{
return
}
$mailboxParams = $Context.TargetObject.GetMailParameters()
foreach ($emailAddress in $mailboxParams.EmailAddresses.GetAddressesByPrefix("smtp"))
{
if ($emailAddress.Address -eq $addressToAdd)
{
return
}
}
# Add new address
if (IsAddressUnique $addressToAdd)
{
AddNewEmailAddress $addressToAdd $mailboxParams
return
}
$addressParts = $addressToAdd.Split("@")
$addressFirstPart = $addressParts[0]
$addressDomainPart = $addressParts[1]
for ($i = 1; $i -le 100; $i++)
{
$uniqueEmailAddress = $addressFirstPart + $i.ToString("00") + "@$addressDomainPart"
if (IsAddressUnique $uniqueEmailAddress)
{
AddNewEmailAddress $uniqueEmailAddress $mailboxParams
return
}
}
For information on how to create Scheduled Tasks, have a look at the following tutorial: https://www.adaxes.com/tutorials_Automa ... gement.htm.