Microsoft 365 – Exchange – Function to Get Transport Rule Properties using PowerShell

By Saad Khamis

November 3, 2021

31310 views

In this blog I will share one way to write a function to get all or specific properties of an Exchange Online transport (mail flow) rule using PowerShell. I will also show how a function can accept pipeline input “|”.

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

Introduction

On multiple occasions I needed to get selected properties of an Exchange Online transport rule. So, I decided to write a function and add it to my PowerShell library.

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, the perfect solution to a challenge or the perfect process to accomplish a task.

Prerequisites

  1. Install Azure PowerShell if you haven’t already. You can use Cloud Shell if you prefer to stay within Azure Portal.
  2. Install Install the EXO V2 module if you haven’t already. 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. Get-TransportRule Use to view transport rules (mail flow rules) in your organization.

Connect to Exchange Online

Use Connect-ExchangeOnline to connect to Exchange Online.

Get a transport (mail flow) rule properties

In its simplest form, Get-TransportRule:

Why a function is needed

  1. to connect to Exchange Online if a connection is not yet established.
  2. to stop and display a single line error message if the transport rule does not exist.
  3. to accept pipeline input.
  4. to 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 Get-TransportRulePropertiesS function

Set up function parameters including pipeline input

The function will have two parameters: (1) transport rule name and (2) properties to be selected.

Next connect to Exchange Online if a connection is not established
And display an error message if the transport rule does not exist then stop
Finally, get the required properties

I used a string variable to store properties to selected to make it easy to use for pipeline and parameter input. You may like to choose a different approach in your function.

I also chose to display each property by itself. You may like to choose a different approach in your function.

Or get all properties

Our complete amazing Get-TransportRulePropertiesS function

Putting all the pieces together produced a nice function. You can add Comment-based Help for a Function.

Putting our amazing function to work

The following examples show different usage of our amazing Get-TransportRulePropertiesS function.

Conclusion

In this blog we explored writing a function to get Transport Rule properties using PowerShell. We connected to Exchange Online if there is no connection. We also displayed an error message if the transport rule does not exist.

Did you find this blog easy to follow and helpful to you? I would love to hear your feedback and suggestions. 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

There's no comments

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 14413 views July 31, 2024

Get all AWS EBS snapshots across all profiles and regions

By Saad Khamis 18770 views June 26, 2024