Script Repository


Disable mail forwarding if forwarding address matches regular expression

October 15, 2015
1706

The script disables mail forwarding for an Exchange mailbox if the forwarding address matches a regular expression.

Parameter:

  • $regex - specifies the regular expression.
Edit Remove
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")


Comments ( 0 )
No results found.
Leave a comment