Skip to content
Menu

Audit Logging Off - Turn on Unified and Mailbox Auditing

Turns on the unified audit log and mailbox auditing when they are off, lists accounts that bypass auditing, and saves the before-state. Never turns auditing off or changes retention.

Audit logging off - turn on unified and mailbox auditing is free for members.

Enter your email to create your free account and get the file.

We'll email you the script and the weekly MSP Outpost email. Unsubscribe anytime. Privacy policy.

Already a member? Sign in.

Be the first to review Audit logging off - turn on unified and mailbox auditing. Tell other MSPs how it worked for you.

Write a Review

LanguagePowerShell
Runs onWindows 10, 11, Server 2016+, with the app certificate; runs as SYSTEM or a user
NeedsPowerShell 5.1; Graph: Organization.Read.All; ExchangeOnlineManagement 3.1+, Exchange.ManageAsApp, Exchange admin role
Changes thingsChanges what it runs on

What It Does

The script connects to Exchange Online as your app and checks three things: whether the unified audit log is ingesting, whether mailbox auditing is on for the organization, and which accounts have mailbox audit bypass, meaning their mailbox actions bypass mailbox auditing. It turns on what is off: the unified audit log and the organization-wide mailbox auditing setting. Because that setting overrides each mailbox's own audit setting, no mailbox is edited one by one.

Bypass accounts are only listed, as a warning, by default, because a bypass is sometimes deliberate, for example a backup or migration service account. Set ClearAuditBypass=true to switch bypass off for every one of them. The script never turns auditing off, never changes which mailbox actions are audited and never changes retention.

Before it changes anything it writes both settings and the bypass accounts to a before-state JSON file under the MSPOutpost data folder on this computer, and prints the path. Afterwards it reads the settings back, retrying for a short while, and ends with an error if one did not take. Audit events can take hours to become searchable after you turn auditing on, so the script checks the settings, not the first events.

A brand-new tenant may not yet be customizable. In that case the script turns on organization customization, stops without changing anything else and tells you to run it again later, often after about an hour. Run it with DryRun=true first to see what would change. A second run after a real change ends OK and says it is already on. If the ExchangeOnlineManagement module is not installed, the run is skipped.

Before You Run It

  • Register an app for this client with a certificate, and keep the certificate with its private key on the computer that runs the script.
  • Give the app the Microsoft Graph application permission Organization.Read.All and the Exchange.ManageAsApp permission.
  • Assign the app an Exchange role that covers the Audit Logs and Organization Configuration roles, for example Exchange Administrator.
  • Install the ExchangeOnlineManagement module (version 3.1 or later) on the computer.
  • Run it once with DryRun=true and read what it would change.
  • Check why any bypass accounts exist before you set ClearAuditBypass=true.
  • If a new tenant makes it ask you to run it again, wait and run it again later.

Type and Risk

  • Type: Action. It makes its change, checks the change worked and says what changed. It exits 0, or 2 when it could not do the job.
  • Risk: Low. Set DryRun=true, or run it with -WhatIf, to list every change without making any.
  • What it changes: Sets UnifiedAuditLogIngestionEnabled true and AuditDisabled false; with ClearAuditBypass also switches mailbox audit bypass off; writes a before-state JSON file.
  • Restart: It never restarts the machine.
  • Typical run: Under a minute.
  • Example answer: "Auditing turned on: unified audit log on, mailbox auditing on, 0 bypass account(s) cleared; before-state saved"
  • 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.
  • ClearAuditBypass: true to also switch mailbox audit bypass off for every account that has it. Default false: bypass accounts are only listed.

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.

The Script

enable-audit-logging.ps1 · 1093 lines · 60.8 KB

Free for Members

Create a free account or sign in to read, copy and download this script.

Welcome Back

Sign in to your MSP Outpost account.