We use cookies to improve your experience.
By your continued use of this site you accept such use.
For more details please see our privacy policy and cookies policy.

Script repository

Disable mail forwarding if forwarding address matches regular expression

October 15, 2015 Views: 3045

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
Leave a comment
Loading...

Got questions?

Support Questions & Answers