Skip to content

Latest commit

 

History

History
59 lines (47 loc) · 4.18 KB

MailboxSizeReportQuotaIf.md

File metadata and controls

59 lines (47 loc) · 4.18 KB

PowerShell Graph SDK Mailbox Size Quota if example

A number of years ago I wrote this QuotaIf script for Exchange 2013 for when your playing around with different Mailbox quota values you might want to see what the usage of those quotas would be before you apply them to a Mailbox. This is a very simple Mailbox Size script for Remote PowerShell to show you this with a funky console graph output (and a CSV report) eg

Example Output

This used the Exchange Management Shell Get-MailboxStatistics cmdlet to get the Mailboxes size. If you wanted to do this in the Graph API you could use the Reporting endpoint for reportRoot: getMailboxUsageDetail - Microsoft Graph v1.0 | Microsoft Docs this in the production endpoint returns a CSV file which if you want to do further processing on you need to parse. In Powershell this is pretty trivial using Import-csv so you can turn the result of the Graph operation back into some PSObjects you can do post processing on. So to change my original script from the EMS cmdlets to the Graph Powershell SDK only required a few minimal lines of change eg

Connect-MgGraph -Scopes "Reports.Read.All" | Out-Null

$TempFileName = [Guid]::NewGuid().ToString() + ".csv"

Invoke-MgGraphRequest -Uri "https://graph.microsoft.com/v1.0/reports/getMailboxUsageDetail(period='D7')" -OutputFilePath ./$TempFileName

$MbReport = Import-csv ./$TempFileName

This will logon to the Graph, get an access token with the correct scope for the Reports Endpoint and then execute a Get request on that Endpoint which will download the csv result which contains the Mailbox sizes to the tempfile in the same directory. The reason I don't use get-mgreportMailboxUsageDetail is because there's been a bug microsoftgraph/msgraph-sdk-powershell#407 for what looks like a very long time. The last line imports that CSV back into a collection and the rest of the code remains unchanged (well I did tidy it up a little).

The full script looks like the following I've put a version up on github https://github.com/gscales/Powershell-Scripts/blob/master/Graph101/GraphSDK/MgQuotaIf.ps1 the value you enter as a Quotaif is in MB so 10GB would be Get-MailboxQuotaIf 10000 if you only want to process in GB then you just need to change the divisor value from 1MB to 1GB in line 17. This script requires the Graph Powershell SDK which you can get from PowerShell Gallery | Microsoft.Graph 1.6.1

function Get-MailboxQuotaIf {
[CmdletBinding()]
param (   
    [Parameter(Position = 1, Mandatory = $true)]
    [Int32]
    $QuotaIfVal
)
Process {
    Connect-MgGraph -Scopes "Reports.Read.All" | Out-Null
    $TempFileName = [Guid]::NewGuid().ToString() + ".csv"
    Invoke-MgGraphRequest -Uri "https://graph.microsoft.com/v1.0/reports/getMailboxUsageDetail(period='D7')" -OutputFilePath ./$TempFileName
    $MbReport = Import-csv ./$TempFileName
    $rptCollection = @()  
    foreach($Mailbox in $MbReport){
        $rptObj = "" | Select MailboxName,TotalSize,QuotaIfPercent,PercentGraph  
        $rptObj.MailboxName = $Mailbox.'Display Name'
        [Int64]$rptObj.TotalSize = $Mailbox.'Storage Used (Byte)'/1MB  
      
        $rptObj.QuotaIfPercent = 0    
        if($rptObj.TotalSize -gt 0){  
            $rptObj.QuotaIfPercent = [Math]::round((($rptObj.TotalSize/$QuotaIfVal) * 100))   
        }  
        $PercentGraph = ""  
        for($intval=0;$intval -lt 100;$intval+=4){
            if($rptObj.QuotaIfPercent -gt $intval){
                $PercentGraph += [char]0x2593
            }
            else{		
                $PercentGraph += [char]0x2591
            }
        }
        $rptObj.PercentGraph = $PercentGraph 
        $rptCollection +=$rptObj   
    }           
    Remove-Item -Path ./$TempFileName
    return, $rptCollection
}
}