What It Does
The script connects to Exchange Online as your app and reads the tenant's accepted domains. It checks user and shared mailbox forwarding settings, plus enabled forwarding and redirect rules unless IncludeInboxRules is false. Use TargetMailbox to check one mailbox by address.
A target counts as external when its domain is not one of the tenant's accepted domains and not in AllowedDomains. A forwarding address that cannot be looked up is reported as external, to be checked by hand. Internal forwarding is not reported.
It writes one row per flagged forwarding setting or rule and ends ALERT when it finds any, naming the first few in the answer. It never changes a mailbox or a rule. Use the matching remove script to turn the forwards off after you have read this list.
The inbox rule pass reads one mailbox at a time, so a large tenant can take several minutes. Hidden inbox rules are not read. If a mailbox's rules cannot be read, the script warns and continues. If the ExchangeOnlineManagement module is missing, it ends SKIPPED.
Before You Run It
- Install ExchangeOnlineManagement 3.1 or later and give the app Exchange.ManageAsApp plus a role that can read inbox rules, such as Exchange Administrator. Global Reader cannot.
- List legitimate destinations, such as an archive service or a sister company, in AllowedDomains so they do not alert.
- Set IncludeInboxRules to false for a fast check, true when you need the full picture.
- Run it before you block external forwarding, and check which listed forwards the proposed policy will affect.
- Schedule it daily or weekly and alert on exit 1.
Type and Risk
- Type: Monitor. It checks one condition. It exits 1 when it finds the problem, so your RMM can alert on it, 0 when all is well, and 2 when it could not run.
- Risk: Low. It reads and changes nothing.
- Restart: It never restarts the machine.
- Typical run: 1 to 10 minutes on a large tenant.
- Example answer: "Forwarding outside: 2 found; [user sign-in] (rule 'Fwd') -> [external address A]; [another user sign-in] (mailbox) -> [external address B]"
- Tested before listing with Windows PowerShell 5.1, run as SYSTEM, against its own test cases and a Microsoft 365 Business Premium test tenant.
Inputs
Pass an input as an argument (-Name value), as an environment variable with the same name, which is how most RMMs pass script variables, or change its default in the param() block at the top of the script. An argument wins over an environment variable, which wins over the default.
- TenantId: The client's tenant ID (a GUID) or domain such as contoso.onmicrosoft.com. Required.
- ClientId: The app registration's Application (client) ID, a GUID. Required.
- CertificateThumbprint: Thumbprint of the app's certificate, with its private key in LocalMachine\My or CurrentUser\My on this computer. Preferred. Leave empty to use the ClientSecret environment variable.
- AllowedDomains: Extra domains that are fine to forward to (for example a sister company or an archive service), separated by comma, semicolon or new line. The tenant's accepted domains are always allowed. Default none.
- IncludeInboxRules: true to also read every mailbox's inbox rules (slower). Default true.
Every script also takes DryRun, OutputFormat (Text, Answer, Json or Csv), ExportPath and ResultField, which writes the answer to an RMM custom field.
License
Provided "as is" under the MIT License, without warranty of any kind. Test before production use. MSP Outpost and User First Digital Inc. accept no liability for any damage or loss.