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)).
ā
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()
- AB21805Apr 18, 2024Bronze ContributorHi, Slightly different error this time!
Method invocation failed because [Microsoft.Exchange.Management.RestApiClient.Unlimited`1[[Microsoft.Exchange.Management.RestApiClient.ByteQuantifiedSize,
Microsoft.Exchange.Management.RestApiClient, Version=15.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]] does not contain a method named 'ToBytes'.
At line:11 char:5
+ if ($mailboxStats.TotalItemSize.ToBytes() -gt 0 -and $mailboxStats.Pr ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Is this working for you?- Minseok_SongApr 18, 2024Iron ContributorI'm using a different version of PowerShell, so I can't give you the exact code for your version.
To change the data types of TotalItemSize and ProhibitSendQuota, you will need to refer to the documentation for the objects that contain these properties or related materials.
If you need help, you can refer to the official documentation for your version of PowerShell or ask someone else for help again.
Thank you.- AB21805Apr 18, 2024Bronze ContributorHi can you confirm what version you are using so I can then try with that?