The script disables mail forwarding for an Exchange mailbox if the forwarding address matches a regular expression.
Parameter:
- $regex - specifies the regular expression.
PowerShell
$regex = "\w+@abc\.com" # TODO: modify me
# Get forwarding recipient
try
{
$altRecipientDN = $Context.TargetObject.Get("altRecipient")
}
catch
{
return
}
# Check the recipient's email address against the regular expression
$object = $Context.BindToObjectByDN($altRecipientDN)
$mail = $object.Get("mail")
if ($mail -notmatch $regex)
{
return
}
# Disable mail forwarding
$mailboxParams = New-Object "Softerra.Adaxes.Adsi.Exchange.AdmExchangeMailboxParameters"
$deliveryOptions = $mailboxParams.MailFlowSettings.DeliveryOptions
$deliveryOptions.IsModificationEnabled = $True
$deliveryOptions.ForwardingAddressModificationEnabled = $True
# Save changes
$Context.TargetObject.SetMailParameters($mailboxParams, "ADM_SET_EXCHANGE_PARAMS_FLAGS_NONE")