Forum Discussion

AB21805's avatar
AB21805
Bronze Contributor
Apr 18, 2024
Solved

Email IT department a report of a specific mailbox reaching 90% storage quota

Hello everyone,   I'm looking for a script that can send a warning email to the IT department when a user's mailbox reaches 90% capacity. I intend to schedule this script to run through a runbook....
  • Minseok_Song's avatar
    Minseok_Song
    Apr 19, 2024

    Hi AB21805, I tested it on EXO V3 and here is the revised code. I found some other errors besides the existing type error, so I fixed the code in general.

    I'll give you the full modified code first and explain it. The modified code works fine in my local environment.

     

     

    # Connect to Exchange Online
    Connect-ExchangeOnline
    
    # Specify the user's mailbox identity
    $mailboxIdentity = "email address removed for privacy reasons"
    
    # Get mailbox configuration and statistics for the specified mailbox
    $mailboxConfig = Get-Mailbox -Identity $mailboxIdentity
    $mailboxStats = Get-MailboxStatistics -Identity $mailboxIdentity
    
    # Check if TotalItemSize and ProhibitSendQuota are not null and extract the sizes
    if ($mailboxStats.TotalItemSize -and $mailboxConfig.ProhibitSendQuota) {
        $totalSizeBytes = $mailboxStats.TotalItemSize.Value.ToString().Split("(")[1].Split(" ")[0].Replace(",", "") -as [double]
        $prohibitQuotaBytes = $mailboxConfig.ProhibitSendQuota.ToString().Split("(")[1].Split(" ")[0].Replace(",", "") -as [double]
    
        # Convert sizes from bytes to gigabytes
        $totalMailboxSize = $totalSizeBytes / 1GB
        $mailboxWarningQuota = $prohibitQuotaBytes / 1GB
    
        # Check if the mailbox size exceeds 90% of the warning quota
        if ($totalMailboxSize -ge ($mailboxWarningQuota * 0.9)) {
            # Send an email notification
            $emailBody = "The mailbox $($mailboxIdentity) has reached $($totalMailboxSize) GB, which exceeds 90% of the warning quota."
    
            Send-MailMessage -To "email address removed for privacy reasons" -From "email address removed for privacy reasons" -Subject "Mailbox Size Warning" -Body $emailBody -SmtpServer "smtp.office365.com" -Port 587 -UseSsl -Credential (Get-Credential)
        }
    } else {
        Write-Host "The required values(TotalItemSize or ProhibitSendQuota) are not available."
    }

     

     

    To test if the mail sending function was working, I temporarily modified the condition to if ($totalMailboxSize -ge ($mailboxWarningQuota * 0.0)).

     

     

Resources