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)).
ā
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
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" } }
- AB21805Apr 18, 2024Bronze Contributor
- Minseok_SongApr 18, 2024Iron Contributor
AB21805 I took a look at the latest documentation and made some changes to the code to match the v3 exo version. In the end, the main cause of the problem was that the TotalItemSize and the ProhibitSendQuota type were not returned in byte form. If you encounter this error in the future, please be aware of it and change the code accordingly. Thank you very much.
# 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-EXOMailboxStatistics -Identity $mailboxIdentity # Check if mailbox size exceeds the warning quota if ($mailboxStats.TotalItemSize.ToBytes() -gt 0 -and $mailboxStats.ProhibitSendQuota.ToBytes() -gt 0) { $totalMailboxSize = $mailboxStats.TotalItemSize.ToBytes() / 1GB $mailboxWarningQuota = $mailboxStats.ProhibitSendQuota.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" } }
Code changes
Get-MailboxStatistics -> Get-EXOMailboxStatistics
TotalItemSize.Value.ToBytes() -> TotalItemSize.ToBytes()
ProhibitSendQuota.Value.ToBytes() -> ProhibitSendQuota.ToBytes()