The script forms a new primary SMTP address based on the prefix of the current primary address and the domain part specified in a parameter. The current primary SMTP address will remain in the list of proxy addresses. To execute the script, create a custom command with a parameter that will be used to specify the domain part for the new primary SMTP address.
In the script, the $suffixParameterName variable specifies the name of the parameter used to enter the new primary SMTP domain part with the param- prefix.
PowerShell
$suffixParameterName = "param-MyParam" # TODO: modify me
# Get parameter value
$suffix = $Context.GetParameterValue($suffixParameterName)
# Get primary SMTP Address
$mailboxParams = $Context.TargetObject.GetMailParameters()
$emailAddresses = $mailboxParams.EmailAddresses
for ($i = 0; $i -lt $emailAddresses.Count; $i++)
{
$emailAddress = $emailAddresses.GetAddress($i,[ref]"ADS_PROPERTY_NONE")
if ($emailAddress.IsPrimary -and $emailAddress.Prefix -eq "smtp")
{
$primarySMTPAddress = $emailAddress.Address
break
}
}
$addressPrefix = $primarySMTPAddress.Substring(0, $primarySMTPAddress.IndexOf("@"))
# Create an instance of the 'AdmExchangeMailboxParameters' class
$mailboxParams = New-Object "Softerra.Adaxes.Adsi.Exchange.AdmExchangeMailboxParameters"
# Disable automatic update of e-mail addresses based on e-mail address policy
$mailboxParams.EmailAddressPolicyEnabled = $False
$emailAddresses = $mailboxParams.EmailAddresses
$emailAddresses.OverrideOldValues = $False
# Create a new email address
$emailAddress = $emailAddresses.CreateAddress("ADM_EXCHANGE_ADDRTYPE_SMTP", $null)
$emailAddress.Address = "$addressPrefix@$suffix"
$emailAddress.IsPrimary = $True
# Add the new email address to the existing list
$emailAddresses.Add("ADS_PROPERTY_APPEND", $emailAddress)
$mailboxParams.EmailAddresses = $emailAddresses
# Save the changes
$Context.TargetObject.SetMailParameters($mailboxParams, "ADM_SET_EXCHANGE_PARAMS_FLAGS_NONE")