Grabbing mailbox usage statistics from Office365 regularly

Office365 now exposes a number of reports which will hopefully be useful for measuring usage and engagement of mailbox users.

One such report is the “Active and inactive mailboxes” report exposed within the Admin Portal:

https___portal_office_com_default_aspx_AllReportsRootV2

In order to retrieve these data over time programatically, I’ve written a Powershell script to grab the data from the Get-MailboxActivityReport cmdlet and store it in a date stamped CSV. This requires a password to be stored (securely) but can then be invoked manually or from Task Scheduler automatically 🙂

 

Script

Param(
[Parameter(Mandatory=$True)] [string] $User,
[Parameter(Mandatory=$True)] [string] $CredentialFile,
[Parameter(Mandatory=$True)] [string] $CSVBase
)

$now = get-date -UFormat "%Y-%m-%dT%H%M%S"

Write-Verbose "Setting up credential object for $user using $credentialfile"
$password = get-content $CredentialFile -ErrorAction Stop | ConvertTo-SecureString -ErrorAction stop
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $password

Write-Verbose "Setting up PowerShell"
Import-Module MSOnline -ErrorAction stop

Write-Verbose "Connecting to MSFT"
Connect-MsolService -Credential $cred -ErrorAction stop
$global:PSSession = New-PSSession -ErrorAction stop -ConfigurationName Microsoft.Exchange `
    -ConnectionUri https://ps.outlook.com/powershell `
    -Credential $cred `
    -Authentication Basic `
    -AllowRedirection

Import-PSSession $PSSession -CommandName Get-MailboxActivityReport -ErrorAction stop

Write-Verbose "Requesting data"
$data = Get-MailboxActivityReport

Write-Verbose "Exporting data"
$data | export-csv -LiteralPath $CSVBase"-"$now".csv"

Storing the password securely

Invoke the following as the user you’re going to run this as:

read-host -assecurestring | convertfrom-securestring | out-file cred.txt

Invoking the script

getmailboxstats.ps1 -user admin@example.com -CredentialFile .\cred.txt -CSVBase mailboxstats

Leave a Reply