Quantcast
Channel: Syed Jahanzaib – Personal Blog to Share Knowledge !
Viewing all articles
Browse latest Browse all 409

Office365 – Get users inbox/archive size report by email

$
0
0

Following powershell script can be executed/scheduled to send below information via Email

  1. Users inbox size which are greater then 10 GB
  2. Users archive box size (enabled archived only)

Sample:


PowerShell Script:

# POWERSHELL script to connect with O365 vis PS commands, & get inbox/archive size & send email
# I have used my local email smtp servers, you can use o365/Gmail etc to send email
# Scripting by Syed.jahanzaib
# aacable DOT wordpress DOT com
# aacable AT hotmail DOT com
# 28-NOV-2022

#Company Name
$COMPANY = "zabbo"
#Define admin users to connect with O365 session
$ADMIN_USER = "admin@example.com"
$DATE = Get-Date -Format "MM-dd-yyyy"
$DATE_FULL = Get-Date
$PATH = "C:\Office365_zaib"
#If folder is not present, create one
If(!(test-path -PathType container $PATH))
{
New-Item -ItemType Directory -Path $PATH
}
cd $PATH

# DEFINE INBOX FETCHING WHICH ARE GREATER THEN THIS ***
$INBOX_SIZE= "10GB"

# DEFINE Output Path/File Names
$U_INB_OUTFILE = "$PATH\$COMPANY-O365-INBOX-SizeReport-$DATE.csv"
$U_ARCH_OUTFILE = "$PATH\$COMPANY-O365-ARCHIVE-SizeReport-$DATE.csv"

# DEFINE MAIL SUBJECT
$SMTP_SERVER = "mail.example.com"
$SMTP_SERVER_PORT = "25"
$SMTP_FROM = "ALERTS@EXAMPLE.COM"
$TO_1 = "SUPPORT1@example.com"
$TO_2 = "SUPPORT2@example.com"

$FOOTER = "Script runtime = $DATE_FULL Powered by example.com IT DEPT. (SJZ)"
$MAIL_SUB = "$COMPANY - O365 Weekly Custom Report @ $DATE"
$MAIL_BODY = "You can see attached CSV Files to view inbox / archive size "

######################################
### DONOT EDIT BELOW THIS LINE - SJZ #
######################################

#Connect to O-365
Connect-ExchangeOnline -UserPrincipalName $ADMIN_USER

#*** GET MAIL BOX LARGER THEN 10 GB
echo "*** GET INBOX MAIL BOX LARGER THEN 10 GB ..."
Get-EXOMailbox -ResultSize Unlimited | Get-EXOMailboxStatistics | Where-Object {[int64]($PSItem.TotalItemSize.Value -replace '.+\(|bytes\)') -gt "$INBOX_SIZE"} | Sort-Object TotalItemSize -Descending | Select-Object DisplayName, ItemCount, TotalItemSize | Export-Csv "$U_INB_OUTFILE" -NoTypeInformation

#*** GET ARCHIVE BOX SIZE, for those whose archive is enabled
# DEFINE VARIABLES TO fetch ACTIVE ARCHIVE box data only
$Result=@()
#Get all user mailboxes
echo "*** GET ARCHIVE MAIL BOX LARGER THEN 10 GB ..."
$mailboxes = Get-Mailbox -ResultSize Unlimited –RecipientTypeDetails UserMailbox
$totalmbx = $mailboxes.Count
$i = 0
$mailboxes | ForEach-Object {
$i++
$mbx = $_
$size = $null

Write-Progress -activity "Processing $mbx" -status "$i out of $totalmbx completed"

if ($mbx.ArchiveStatus -eq "Active"){
#Get archive mailbox statistics
$mbs = Get-MailboxStatistics $mbx.UserPrincipalName -Archive

if ($mbs.TotalItemSize -ne $null){
$size = [math]::Round(($mbs.TotalItemSize.ToString().Split('(')[1].Split(' ')[0].Replace(',','')/1MB),2)
}else{
$size = 0 }
}

$Result += New-Object -TypeName PSObject -Property $([ordered]@{
UserName = $mbx.DisplayName
UserPrincipalName = $mbx.UserPrincipalName
ArchiveStatus =$mbx.ArchiveStatus
ArchiveName =$mbx.ArchiveName
ArchiveState =$mbx.ArchiveState
ArchiveMailboxSizeInMB = $size
ArchiveWarningQuota=if ($mbx.ArchiveStatus -eq "Active") {$mbx.ArchiveWarningQuota} Else { $null}
ArchiveQuota = if ($mbx.ArchiveStatus -eq "Active") {$mbx.ArchiveQuota} Else { $null}
AutoExpandingArchiveEnabled=$mbx.AutoExpandingArchiveEnabled
})
}
#$Result | Export-CSV "$U_ARCH_OUTFILE" -NoTypeInformation -Encoding UTF8

# FINALY FEED THE RESULTS TO OUR OUTPUT FILE (VARIABLES ARE DEFINE ABOVE) (SJZ)
$Result | Where-Object { $_.ArchiveStatus -eq "Active" } | Select UserName, UserPrincipalName, ArchiveMailboxSizeInMB, ArchiveWarningQuota, ArchiveQuota | Export-CSV "$U_ARCH_OUTFILE" -NoTypeInformation -Encoding UTF8

#***SEND EMAIL with MULTI attachments (SJZ)
Send-MailMessage -BodyAsHtml -SmtpServer $SMTP_SERVER -Port $SMTP_SERVER_PORT -From $SMTP_FROM -To $TO_1,$TO_2 -Subject "$MAIL_SUB" -Body "<br /> $MAIL_SUB <br /> <br /> $MAIL_BODY <br /> <br /> $FOOTER ` " -Attachments "$U_INB_OUTFILE","$U_ARCH_OUTFILE"

Task Scheduler Setting:


Regard’s
Syed Jahanzaib


Viewing all articles
Browse latest Browse all 409

Trending Articles