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 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)).
- AB21805Apr 19, 2024Bronze Contributor
Minseok_Song Also looks like there is an error come this part
# Check if TotalItemSize and ProhibitSendQuota are not null and extract the sizesif ($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 quotaif ($totalMailboxSize -ge ($mailboxWarningQuota * 0.0)) {# 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."}As when I run$organization = "mydomain.co.uk"Connect-ExchangeOnline -ManagedIdentity -Organization $organization# 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 $mailboxIdentityI get no error. - AB21805Apr 19, 2024Bronze Contributor
Hi,
I am getting this now
System.Management.Automation.ParameterBindingException: A parameter cannot be found that matches parameter name 'UseManagedIdentity'.
at System.Management.Automation.CmdletParameterBinderController.VerifyArgumentsProcessed(ParameterBindingException originalBindingException)
at System.Management.Automation.CmdletParameterBinderController.BindCommandLineParametersNoValidation(Collection`1 arguments)
at System.Management.Automation.CmdletParameterBinderController.BindCommandLineParameters(Collection`1 arguments)When I use this / test:
Minseok_Song So It seems to communicate with exchange.
i did try adding the first screenshot part:
$organization = "mydomain.co.uk"Connect-ExchangeOnline -ManagedIdentity -Organization $organizationBut same error I posted first - Minseok_SongApr 19, 2024Iron Contributor
AB21805 If you want to connect to Exchange Online using a managed identity in Azure Automation, you must specify the -UseManagedIdentity flag, which works without the additional Credential parameter.
Original code
Connect-ExchangeOnline
Modified code
Connect-ExchangeOnline -UseManagedIdentity -Verbose
And if you're using Azure Automation, my understanding is that you need to use Azure Automation to get the credentials, and you can do that by modifying the code that sends the email like this.
Original code
# 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)
Modified code
# Automation Account Credential $credential = Get-AutomationPSCredential -Name 'SMTPCredential' # 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 $smtpCredential
I've provided the code below for you to try out. Since I'm not currently able to test this code myself, it may require some adjustments, especially in the credential methods, to meet your specific needs.
# Connect to Exchange Online using Managed Identity Connect-ExchangeOnline -UseManagedIdentity -Verbose # 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)) { # Retrieve SMTP credentials from Azure Automation Credential $smtpCredential = Get-AutomationPSCredential -Name 'SMTPCredential' # 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 $smtpCredential } } else { Write-Host "The required values(TotalItemSize or ProhibitSendQuota) are not available." }
- AB21805Apr 19, 2024Bronze Contributor
Thank you so much! The script works! Do I need to change much to log in with a managed identify (system assigned) within a runbook for exchange online - ( the identiy already has permission) and module installed.
I keep getting this!
System.Management.Automation.ParameterBindingException: Cannot process command because of one or more missing mandatory parameters: Credential.
at System.Management.Automation.CmdletParameterBinderController.PromptForMissingMandatoryParameters(Collection`1 fieldDescriptionList, Collection`1 missingMandatoryParameters)
at System.Management.Automation.CmdletParameterBinderController.HandleUnboundMandatoryParameters(Int32 validParameterSetCount, Boolean processMissingMandatory, Boolean promptForMandatory, Boolean isPipelineInputExpected, Collection`1& missingMandatoryParameters)
at System.Management.Automation.CmdletParameterBinderController.BindCommandLineParameters(Collection`1 arguments)
at System.Management.Automation.CommandProcessor.BindCommandLineParameters()
at System.Management.Automation.CommandProcessor.Prepare(IDictionary psDefaultParameterValues)
at System.Management.Automation.CommandProcessorBase.DoPrepare(IDictionary psDefaultParameterValues)
at System.Management.Automation.Internal.PipelineProcessor.Start(Boolean incomingStream)
at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input)
at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] commandRedirections, FunctionContext funcContext)
at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) - Minseok_SongApr 19, 2024Iron Contributor
AB21805I'll outline the code changes below and detail the specific problems I encountered and solved.
1. Type Error
Firstly, there was a type error in the original code, which I have corrected as follows:
Original code
$mailboxStats.TotalItemSize $mailboxStats.ProhibitSendQuota
Modified code
$totalSizeBytes = $mailboxStats.TotalItemSize.Value.ToString().Split("(")[1].Split(" ")[0].Replace(",", "") -as [double] $prohibitQuotaBytes = $mailboxConfig.ProhibitSendQuota.ToString().Split("(")[1].Split(" ")[0].Replace(",", "") -as [double]
2. method error
There was a problem where ProhibitSendQuota was called on the wrong object, which I have fixed in the modified code below. This may have been one of the reasons why the code did not work correctly the first time you ran it.
Original code
$mailboxStats = Get-MailboxStatistics -Identity $mailboxIdentity $mailboxStats.TotalItemSize $mailboxStats.ProhibitSendQuota
Modified code
$mailboxConfig = Get-Mailbox -Identity $mailboxIdentity $mailboxStats = Get-MailboxStatistics -Identity $mailboxIdentity $mailboxStats.TotalItemSize $mailboxConfig.ProhibitSendQuota
3. Credential error
I encountered an authentication-related error when sending mail, so I made some adjustments to the port number and updated the code accordingly.
Original code
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"
Modified code
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)
Finally, I made some changes to the logic. While testing the code, I noticed errors when variables like ProhibitSendQuota came in as null, so I adjusted the code to handle such cases more gracefully.