Enable mailbox audit logging Office 365 with PowerShell
Enable mailbox audit logging Office 365 with PowerShell.
Mailbox audit logging helps administrator to keep track of changes that are made on mailboxes but not only limited to that. Audit logging of mailboxes helps troubleshooting of emails. The reason that I wrote the below script, was to enable mailbox audit logging automatically on new mailboxes in the system. All mailboxes have enabled the audit logging and with this script that is running everyday, audit logging is enabled for all new mailboxes that will be created.
Audit logging can be enabled on different levels. In the script, full mailbox audit logging has been enabled. Changes made by Owner, Delegate person and Admin will be logged. Owner is the actual owner of the mailbox. Delegate is the any other user that permissions have provided to him / her on the specific mailbox. Admin is any administrator on exchange that has access to perform changes on the mailbox if there is a delegation.
Script runs on Azure Automation
The below script has been set up to run directly on Microsoft Azure. Microsoft Azure provides you with the ability to have scripts run directly on it. Under Azure automation you can have your runbooks. Runbooks can be a plain PowerShell script but also it allows you to create a runbook visually. In Azure automation credentials are saved in a secure way and can be retrieved from any runbook / script that you have configured. You can have multiple credentials saved and you are able to use any credentials that you may need in a script. When you run a script on Azure automation, a virtual environment is launched at the back-end there is no need to have a VM set up to run the scripts on it.
This next part is provided by Microsoft:
Azure Automation delivers a cloud-based automation and configuration service that provides consistent management across your Azure and non-Azure environments. It consists of process automation, update management, and configuration features. Azure Automation provides complete control during deployment, operations, and decommissioning of workloads and resources.
In general the script will gather everyday all mailboxes with audit logging disabled and will enable it.
Let’s see in more detail what the script does
Firstly, the script will save two credentials in two different variables. The first one are the credentials that are saved in Azure automation for the admin user that will be used to connect and perform the changes. The second one are the credentials that are saved in Azure Automation for the user that will be used to send emails reports.
The next part, I found that is an important part for me not to have any issues with the Azure Automation and run of the scripts. Sometimes the script session hangs and stays active and this might create issues on the next run of the script. So the script checks if there is any active session and closes the session. Then the script will connect and import the session for Exchange Online using the admin credentials saved before.
After the connection to Exchange Online, the script will save all mailboxes in a variable. This variable ($Mailboxes) is used then to keep the UniversalPrincipalNames (UPN) of the mailboxes. I am using the UPN as this attributes is unique and will not create any conflicts during the run. When the list of UPNs is ready, a foreach loop is used to enable mailbox audit logging Office 365. Please note that all options are used in audit logging to keep full audit logs on all levels.
If there will be any errors during the run of the script, an email report will be sent including all the errors or exceptions that may appear.
You can download the script here or copy it from below.
Let me know if there are any questions or comments below.
- Email error variable in PowerShell
- Azure Automation Overview | Microsoft Docs
- Credential assets in Azure Automation | Microsoft Docs
- Get-PSSession – Microsoft Docs
- Remove-PSSession – Microsoft Docs
- New-PSSession – Microsoft Docs
- Import-PSSession – Microsoft Docs
- Out-Null – Microsoft Docs
- Get-Mailbox – TechNet – Microsoft
- Set-Mailbox – TechNet – Microsoft
- Send-MailMessage – Microsoft Docs
- Get-Date – Microsoft Docs
Solution / Script: