Introduction
Microsoft GRAPH REST API provides access to the configuration of many MS365 services.
This article shows how to access MS365 usage reports using this API and Powershell.
Installing the PowerShell GRAPH SDK
Install the GRAPH SDK from the PSGallery provider.
Use the following PowerShell command to install the GRAPH SDK PowerShell.
Install-Package Microsoft.Graph -force -Source PSGallery
These modules contain 8334 commands.
(get-command -Module microsoft.graph*).count
Configure app permissions
To access GRAPH reports in PowerShell, you must give the “Reports.Read.All” delegate right to the “Microsoft Graph Powershell” application.
To do this, you can connect for the first time by specifying the scope.
Connect-MgGraph -Scopes "Reports.Read.All"
Once the authorization request is accepted, the right is visible in the administration of enterprise applications.
Access GRAPH reports in PowerShell
It is then possible to use the commands relating to reports but some pose problems at the level of the “OutFile” parameter. It is for this reason that it is currently necessary to privilege the use of the command “Invoke-MgGraphRequest”.
The list of available GRAPH reports are referenced at Microsoft.
https://docs.microsoft.com/en-us/graph/api/resources/report?view=graph-rest-1.0
To download these reports in PowerShell, you can take inspiration from the following examples. The reports in the CSV format will be downloaded in the current directory.
To count the mailboxes and active mailboxes of the tenant:
Invoke-MgGraphRequest -Uri "https://graph.microsoft.com/v1.0/reports/getMailboxUsageMailboxCounts(period='D7')" -InferOutputFileName
To see the evolution over 7 days of the storage volume of the tenant’s mailboxes:
Invoke-MgGraphRequest -Uri "https://graph.microsoft.com/v1.0/reports/getMailboxUsageStorage(period='D7')" -InferOutputFileName
To see the evolution of the number of mailboxes reaching quota limits:
Invoke-MgGraphRequest -Uri "https://graph.microsoft.com/v1.0/reports/getMailboxUsageQuotaStatusMailboxCounts(period='D7')" -InferOutputFileName
To see the size of the mailboxes and the number of items, anonymously:
Invoke-MgGraphRequest -Uri "https://graph.microsoft.com/v1.0/reports/getMailboxUsageDetail(period='D7')" -InferOutputFileName
To see the evolution of the number of emails sent, received and read by all of the tenant’s mailboxes:
Invoke-MgGraphRequest -Uri "https://graph.microsoft.com/v1.0/reports/getEmailActivityCounts(period='D7')" -InferOutputFileName
To see the evolution of the number of mailboxes that have sent, received or read messages:
Invoke-MgGraphRequest -Uri "https://graph.microsoft.com/v1.0/reports/getEmailActivityUserCounts(period='D7')" -InferOutputFileName
To see the number of emails sent, received and read per mailbox, anonymously:
Invoke-MgGraphRequest -Uri "https://graph.microsoft.com/v1.0/reports/getEmailActivityUserDetail(period='D7')" -InferOutputFileName
Lionel TRAVERSE
Microsoft 365 Certified Administrator Expert
Microsoft Certified Trainer
lionel.traverse@admin365.fr