Forum Discussion

AB21805's avatar
AB21805
Bronze Contributor
Apr 18, 2024

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.

I've come across a script that runs, but it doesn't seem to trigger any action; I don't receive any emails or notifications. It's evident that I'm missing something or doing something incorrectly. Any suggestions or ideas would be greatly appreciated.

 

 

# Connect to Exchange Online
Connect-ExchangeOnline

# Specify the user's mailbox identity
$mailboxIdentity = "email address removed for privacy reasons"

# Get mailbox statistics for the specified mailbox
$mailboxStats = Get-MailboxStatistics -Identity $mailboxIdentity

# Check if mailbox size exceeds the warning quota
if ($mailboxStats.TotalItemSize -gt 0 -and $mailboxStats.ProhibitSendQuota -gt 0) {
$totalMailboxSize = $mailboxStats.TotalItemSize / 1GB
$mailboxWarningQuota = $mailboxStats.ProhibitSendQuota / 1GB

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"
}
}

 

 

  • 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)).

     

    ā€ƒ

     

  • Minseok_Song's avatar
    Minseok_Song
    Iron Contributor

    Hi AB21805 ,

    I've reviewed the code you provided and noticed a problem with the data types used. I understand that the values returned for TotalItemSize and ProhibitSendQuota are not simple numbers, but complex objects that contain data capacity (ByteQuantifiedSize type). This would make direct comparisons between them problematic.

    I'd like to propose a modification to the script that solves this problem by properly converting these values into a comparable format. I'll post the revised code in the comments below.

    • Minseok_Song's avatar
      Minseok_Song
      Iron Contributor

      Below is the modified code

       

       

      # Connect to Exchange Online
      Connect-ExchangeOnline
      
      # Specify the user's mailbox identity
      $mailboxIdentity = "email address removed for privacy reasons"
      
      # Get mailbox statistics for the specified mailbox
      $mailboxStats = Get-MailboxStatistics -Identity $mailboxIdentity
      
      # Check if mailbox size exceeds the warning quota
      if ($mailboxStats.TotalItemSize -gt 0 -and $mailboxStats.ProhibitSendQuota -gt 0) {
          $totalMailboxSize = $mailboxStats.TotalItemSize.Value.ToBytes() / 1GB  
          $mailboxWarningQuota = $mailboxStats.ProhibitSendQuota.Value.ToBytes() / 1GB  
      
          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"
          }
      }

       

       


      Before modification

      $totalMailboxSize = $mailboxStats.TotalItemSize / 1GB
      $mailboxWarningQuota = $mailboxStats.ProhibitSendQuota / 1GB

       

      After modification

      $totalMailboxSize = $mailboxStats.TotalItemSize.Value.ToBytes() / 1GB
      $mailboxWarningQuota = $mailboxStats.ProhibitSendQuota.Value.ToBytes() / 1GB

       

      Thank you

       

      Best regards,

      Minseok Song

      • AB21805's avatar
        AB21805
        Bronze Contributor

        Hi !Minseok_Song 

         

        Thanks for this now I am getting error? 

         

         

        Is this because the limit has now been reached yet? 

         

Resources