Forum Discussion
Email IT department a report of a specific mailbox reaching 90% storage quota
- 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)).
ā
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_SongApr 18, 2024Iron 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 / 1GBAfter modification
$totalMailboxSize = $mailboxStats.TotalItemSize.Value.ToBytes() / 1GB
$mailboxWarningQuota = $mailboxStats.ProhibitSendQuota.Value.ToBytes() / 1GBThank you
Best regards,
Minseok Song
- AB21805Apr 18, 2024Bronze Contributor
Hi !Minseok_Song
Thanks for this now I am getting error?
Is this because the limit has now been reached yet?
- Minseok_SongApr 18, 2024Iron Contributor
AB21805 I've checked my proposed code and found some places where it might cause additional type issues, I'll re-propose the code below with those fixes, thanks.
# 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.Value.ToBytes() -gt 0 -and $mailboxStats.ProhibitSendQuota.Value.ToBytes() -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" } }