Transport Rule Properties updates using PowerShell

By Saad Khamis

November 7, 2021

39609 views

In this blog I will share one way to write a function to update, add or remove values, a specific property of an Exchange Online transport (mail flow) rule using PowerShell.

Note: Function included in this post works with parameters accept string[].

Introduction

My blogs have relatively simple, and sometimes complex, examples. I’m hoping that you will be able to tailor them to your need or use them in your own scripts.

The goal of this blog is to show one way to accomplish a task. It is not to show how to write a perfect script, solution or process to accomplish a task.

Prerequisites

  1. First thing to remember is to install Azure PowerShell. Otherwise, you can use Cloud Shell if you prefer to stay within Azure Portal.
  2. Secondly, install Install the EXO V2 module. Otherwise, you can use Cloud Shell if you prefer to stay within Azure Portal.

PowerShell Cmdlets

  1. Connect-ExchangeOnline Used in the Exchange Online PowerShell V2 module to connect to Exchange Online PowerShell using modern authentication. This cmdlet works for MFA or non-MFA enabled accounts.
  2. Set-TransportRule Use to view transport rules (mail flow rules) in your organization.

Connect to Exchange Online

How to connect to Exchange Online.

Get a transport (mail flow) rule properties

In its simplest form.

For the cmdlet parameters values, you can enter multiple values separated by commas. Use quotation marks if the values contain spaces: “Value1″,”Value2″,…”ValueN”. Review Set-TransportRule.

Why a function is needed

  1. Connect to Exchange Online if a connection is not yet established.
  2. Stop and display a single line error message if the transport rule does not exist.
  3. Accept pipeline input.
  4. Add the code to a library to be loaded at PowerShell startup.
  5. other reasons that I already forgot.

Let us have fun and start writing our function.

Our Set-TransportRulePropertiesS function

Firstly, set up function parameters including pipeline input

The function will have four parameters: (1) transport rule name and (2) property to be set/updated (3) values to be added (4) values to be removed.

Secondly, Connect to Exchange Online if a connection is not established
Thirdly, Check if transport rule exist

Save existing property values in $NewValues variable.

Fourthly, Code snippet to add values to a property

Code snippet to add values to a property.
Finally, Code snippet to remove values from a property
Code snippet to remove values from a property.

Without delay, our complete amazing Set-TransportRulePropertiesS function

Putting all the pieces together produce a nice function. What’s more, you can add Comment-based Help for a Function.

Putting our amazing function to work

Examples to illustrate the usage of our amazing Get-TransportRulePropertiesS function.

Example shows how to add and remove values to an email transport rule.

In conclusion

In summary, we explored writing a function to update Transport Rule properties that accepts String[] using PowerShell. We also connected to Exchange Online if there was no connection. And also if the rule does not exist, an error message is displayed.

Note: Function works with parameters accept string[].

Did you find this blog easy to follow and helpful to you? I certainly would love to hear your feedback and suggestions. So, let me know in the comments below. Happy PowerShelling.

Disclaimer

Purpose of the code contained in blog is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.

Comments

4 comments on “Transport Rule Properties updates using PowerShell

  1. Right on! It was very difficult to find that each rule has conditions that need to be queried by name. Lots of searching before i found your script. Even ChatGPT is not aware of these options yet.
    Thanks!!!

    1. Yes, the function can be modified to accommodate this option.

      A quick solution would be to query all existing transport rules then use a ForEach with the above function to remove UPN.

      Here is an example:

      $RemoveUPN = “removeme.com”
      $TransportRules = Get-TransportRule -ResultSize Unlimited
      ForEach ($TransportRule in $TransportRules) {
      # Remove values
      Set-TransportRulePropertiesS -RuleName $TransportRule.Name
      -Property "ExceptIfSenderDomainIs"

      -RemoveValues $RemoveUPN
      }

      I hope I answered your question. Good luck.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

List all AWS Certificate Manager certificates

By Saad Khamis 14110 views July 31, 2024

Get all AWS EBS snapshots across all profiles and regions

By Saad Khamis 18468 views June 26, 2024