Script Repository


Create transport rule to block mail to Exchange recipient

January 11, 2017
1721

The script creates an Exchange transport rule that blocks incoming mail to a recipient. You can use it, for example, to block mail to disabled users.

To use the script with Adaxes, you can add it to a Business Rule, Custom Command or Scheduled Task using the Run a program or PowerShell script action.

Parameters:

  • $exchangeServer - specifies the fully qualified domain name (FQDN) of your Exchange Server;
  • $ruleName - specifies a name for the new transport rule;
  • $rejectMessageEnhancedStatusCode - specifies a status code that will be returned by Exchange when rejecting messages to the recipient;
  • $rejectMessageReasonText - specifies a reason that explains why the message was rejected.
You can use value references (e.g. %name%) in the rule name and reject message text. When the script is executed, the value references are replaced with property values of the recipient. For example, if you specify the following name for the transport rule: Block mail to %name%, and execute the script on a user mailbox whose name is John Doe, the resulting rule name will be Block mail to John Doe.
Edit Remove
PowerShell
$exchangeServer = "exchangeserver.example.com" # TODO: modify me
$ruleName = "Block mail to %name%" # TODO: modify me
$rejectMessageEnhancedStatusCode = "5.7.929" # TODO: modify me
$rejectMessageReasonText = "Regretfully the user that you attempted to contact is no longer with Contoso Corporation" # TODO: modify me

try
{
    $session = New-PSSession -connectionURI "http://$exchangeServer/powershell" -ConfigurationName Microsoft.Exchange
    Import-PSSession -session $session -DisableNameChecking -AllowClobber
    New-TransportRule $ruleName `
        -SentTo "%distinguishedName%" `
        -RejectMessageEnhancedStatusCode $rejectMessageEnhancedStatusCode `
        -RejectMessageReasonText $rejectMessageReasonText `
        -Enabled $True
}
finally
{
    Remove-PSSession -Session $session
}

Comments ( 0 )
No results found.
Leave a comment