Share via


Mailbox Usage Report and Send email to Exchange Admin

Launch PowerShell and create secure password string file for smtp authentication. The process only once.

 Read-Host -AsSecureString | ConvertFrom-SecureString | Out-File "C:\scripts\checkstatus"
 
Then save the following powershell script in “C:\scripts” directory.
If you want to run this script on a regular basis, you can create “Task Scheduler” task.

# Add Exchange snapin if not already loaded
if (!(Get-PSSnapin | where {$_.Name -eq "Microsoft.Exchange.Management.PowerShell.E2010"}))
{
Write-Verbose "Loading the Exchange 2010 snapin"
try
{
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction STOP
}
catch
{
#Snapin not loaded
Write-Warning $_.Exception.Message
EXIT
}
. $env:ExchangeInstallPath\bin\RemoteExchange.ps1
Connect-ExchangeServer -auto -AllowClobber
}
 
 
# Create Secure Password String for smtp authentication.
# Read-Host -AsSecureString | ConvertFrom-SecureString | Out-File "C:\scripts\checkstatus"
 
 
$now = Get-date
$GetMailboxes = Get-Mailbox -ResultSize unlimited
$Result = @()
foreach ($GetMailbox in $GetMailboxes) {
$StorageLimitStatus = (Get-MailboxStatistics -Identity $GetMailbox).StorageLimitStatus
if ($StorageLimitStatus -ne "BelowLimit") {
$MailboxObject = New-Object PSObject
$MailboxObject | Add-Member NoteProperty -Name "Name" -Value $GetMailbox.Name
$MailboxObject | Add-Member NoteProperty -Name "StorageLimitStatus" -Value $StorageLimitStatus
$Result += $MailboxObject
}
}
 $Result
 
if ($Result) {
$Style = "<style>"
$Style = $Style + "BODY{background-color:White;}"
$Style = $Style + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$Style = $Style + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:MidnightBlue}"
$Style = $Style + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black}"
$Style = $Style + "</style>"
$SendMail = @($Result | Select-Object Name, StorageLimitStatus | ConvertTo-HTML -head $Style -body "<H2>Mailbox Storage Limit Status</H2>" )
 
# Mail from address
$From = "MailboxAlert@contoso.com"
 
# Recipients email addresses
$Recipients = "fatih@contoso.com","ExchangeAdmins@contoso.com"
$To = $Recipients[0]
 
# SMTP Server Name or Ip Address
$SMTPServer = "127.0.0.1"
 
# SMTP Server Listen Port
$SMTPPort = "587"
 
# SMTP Server Logon Name
$Username = "MailboxAlert@contoso.local"
$Password = type "C:\scripts\checkstatus" | ConvertTo-SecureString
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username, $Password
 
$subject = "Mailbox Statistics" 
$body = "Creation time $now."
$body += "`n"
$body += $SendMail
$colorTagTable = @{
ProhibitSend = ' bgcolor="#FF0000">ProhibitSend<';
IssueWarning = ' bgcolor="#FFFF00">IssueWarning<'
}
$colorTagTable.Keys | foreach { $body = $body -replace ">$_<",($colorTagTable.$_) }
 
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { return $true }
$message = New-Object System.Net.Mail.MailMessage $From, $To
$message.Subject = $subject
$message.IsBodyHTML = $true
$message.Body = $body
foreach ($Recipient in $Recipients) {
if ($Recipient -ne $To){
$message.To.add($Recipient)
}
}
 
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.EnableSSL = $true
$smtp.Credentials =  $Credential
$smtp.Send($message)
}