Editing an Exchange Online Mail Flow rule

28 Mar

I needed to be able to update a given mail flow rule in Exchange Online.

The current rule, “If the recipient’s address includes these words then do the following, reject the message with the explanation…”

I wanted to add an email address to this list if it didn’t already exist, so here’s the Powershell I came up with:


$list = new-object collections.arraylist
$list += Get-TransportRule "Mail Flow Rule Name" | select -expand RecipientAddressContainsWords

#check it's not already in the list
if(!($list -contains $emailaddress)){
    $list += $emailaddress 
    Set-TransportRule -Identity "Mail Flow Rule Name" -RecipientAddressContainsWords $list
     #write-host "already in the list"


So, it turns out that any mail flow rule list can contain a maximum of 8192 characters, which I hit quite quickly. So now I have it split into multiple rules, and creating a new rule if required, it needs a tidy up, but the code is as follows:


$myusername = "email@address.com"
$mypassword = "password"

$baserulename = "Block Address"

$securepassword = ConvertTo-SecureString -string $mypassword -AsPlainText -Force 
$cred = new-object System.Management.Automation.PSCredential ($myusername, $securepassword)

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $cred -Authentication Basic –AllowRedirection
Import-PSSession $Session

$stayinloop = $true

$loopint = 0

    $loopint += 1
    $temptransportrulename = $baserulename + $loopint
    if(Get-TransportRule $temptransportrulename){
        #rule exists, lets see how big it is
        write-host "Rule exists $temptransportrulename"
        $list = new-object collections.arraylist
        $list += Get-TransportRule $temptransportrulename | select -expand RecipientAddressContainsWords

        write-host "list count: " + $list.count
        if($list.count -ge 100){
            #too big need another transport rule, lets go and see if the next one exists
               write-host "list count too big for [$temptransportrulename], lets go to next rule"
            #size ok, lets use this one
                write-host "list count ok $temptransportrulename"
                $list += $emailaddress 
                write-host "adding $emailaddress to $temptransportrulename"
                Set-TransportRule -Identity $temptransportrulename -RecipientAddressContainsWords $list
                $stayinloop = $false

        #rule doesnt exist we need to create a new transport rule and add this address to it
        write-host "*** Creating new transport rule $temptransportrulename"
        $emptylist = new-object collections.arraylist
        $emptylist += $emailaddress
        New-TransportRule -Name $temptransportrulename -RejectMessageReasonText "This email address is in the unsubscribe list. Please remove it from all systems." -RejectMessageEnhancedStatusCode "5.7.1" -RecipientAddressContainsWords $emptylist -Enabled $true -Priority $loopint -StopRuleProcessing $true
        $stayinloop = $false