About the script
This script is written in a simple logic. This will give you very basic statistics about an email messages which are sent and received by your organization. We can enable email function, if you want to get the report as an email format or it will just generate an html file and store it in a location from where you are running this script. You have to give start and end date to capture the statistics.
Script will capture the below parameters
1. Total email sent/received
2. Total email size sent/received
3. Top sender/receiver
4. Top sized email sent/received by your exchange server
Future Enhancements
1. Will add more parameters for the statistics
2. Will add the graph/chart
Script Block
#Script Begins
$StartDate = Read-Host "Enter Start Date in MM/DD/YYYY Format"
$EndDate = Read-Host "Enter End Date in MM/DD/YYYY Format"
# Inputs for Email
$Subject = "Email Statistics Report Generated on" + $(Get-Date).toshortdatestring() + " at " + $(Get-Date).ToshorttimeString()
$FromAddress = "admin@lyncit.net"
$ToAddress = "john@lyncit.net"
$Relay = "ex01.lyncit.net"
$SMTPClient = New-Object System.Net.Mail.smtpClient
$MailMessage = New-Object System.Net.Mail.MailMessage
# Mail Function
$Mail = "True"
$body = $null
$body += '<style>'
$body += '<!-- '
$body += 'body { font: Calibri } '
$body += 'table { font: 11pt Calibri; border: 1px} '
$body += 'td { border: 1px ridge white; padding-left: 1px; padding-right: 1px } '
$body += 'p.header { font: 11pt Verdana; color: DarkBlue }'
$body += 'p.normal { font: 8pt Calibri; color: CadetBlue }'
$body += '-->'
$body += '</style>'
$body += '</head>'
$body += '<body>'
#$body += "<p class=`"header`" align='center'><strong>Email Statistics Report Generated on: " + $(Get-Date).toshortdatestring() + " at " + $(Get-Date).ToshorttimeString()
$body += "<p class=`"header`" align='center'><strong>Email Statistics Report between " + $StartDate + " and " + $EndDate
$msgsent = Get-Exchangeserver | where {$_.isHubTransportServer -eq $true -or $_.isMailboxServer -eq $true} | Get-Messagetrackinglog -start $StartDate -end $EndDate -EventID "RECEIVE" -resultsize unlimited | Select-Object Timestamp,Clienthostname,eventid,source,sender,@{Name="Recipients";Expression={$_.recipients}},Recipientcount,serverhostname,totalbytes,messagesubject
$msgreceive = Get-Exchangeserver | where {$_.isHubTransportServer -eq $true -or $_.isMailboxServer -eq $true} | Get-Messagetrackinglog -start $StartDate -end $EndDate -EventID "DELIVER" -resultsize unlimited | Select-Object Recipients,totalbytes
$msg1 = $msgsent | Measure-Object totalbytes -Maximum -minimum -average -sum
$msgsentcount = $msgsent.count
$msgsentsize = $msg1.sum / (1024 * 1024)
$size = "{0:N2}" -f $msgsentsize + "MB"
$bigsent = $msg1.maximum / (1024 * 1024)
$bigsentmb = "{0:N2}" -f $bigsent + "MB"
$msg2 = $msgreceive | Measure-Object totalbytes -Maximum -minimum -average -sum
$msgrcount = $msgreceive.count
$msgrsize = $msg2.sum / (1024 * 1024)
$rsize = "{0:N2}" -f $msgrsize + "MB"
$bigr = $msg2.maximum / (1024 * 1024)
$bigrmb = "{0:N2}" -f $bigr + "MB"
Write-Host $msgsentcount
Write-Host $size
if ($msgsent -ne $null)
{
#Table Header
$body += '<table width=80% align=center>'
$body += '<td colspan=2 align=center style="background-color: #DCDCDC" border: 1 ; ><strong><font color= Black>' + "SENT EMAILS STATISTICS" + '</font></strong></td></tr>'
$body += '<tr><td align="Center" style="background-color: #736F6E" width="13%" border: 1 Groove white;><p class="table" style="color: white"><b>Parameters</b></p></td>'
$body += '<td align="Center" style="background-color: #736F6E" width="10%" border: 1 Groove white;><p class="table" style="color: white"><b>Value</b></p></td>'
$body += '<tr><td align="Center" border: 1 Groove white;>' + "Total Emails Sent" + "</td>"
$body += '<td align="Center" border: 1 Groove white;>' + $msgsentcount + "</td></tr>"
$body += '<tr><td align="Center" border: 1 Groove white; >' + "Total Sent Emails Size" + "</td>"
$body += '<td align="Center" border: 1 Groove white;>' + $size + "</td></tr>"
$body += '<tr><td align="Center" border: 1 Groove white; >' + "Top sent Email size" + "</td>"
$body += '<td align="Center" border: 1 Groove white;>' + $bigsentmb + "</td></tr>"
$senders = $msgsent | Group-Object Sender | Sort-Object Count -Descending | Select-Object -first "10"
$body += '<td colspan=2 align=center style="background-color: #DCDCDC" border: 1 ; ><strong><font color= Black>' + "Top 10 Senders" + '</font></strong></td></tr>'
$body += '<tr><td align="Center" style="background-color: #736F6E" width="13%" border: 1 Groove white;><p class="table" style="color: white"><b>Name</b></p></td>'
$body += '<td align="Center" style="background-color: #736F6E" width="10%" border: 1 Groove white;><p class="table" style="color: white"><b>Sent Message Count</b></p></td>'
foreach ($SenderEntry in $senders)
{
$name = $senderentry.name
$count = $senderentry.count
$body += '<tr><td align="Center" border: 1 Groove white;>' + $name + "</td>"
$body += '<td align="Center" border: 1 Groove white; >' + $count + "</td></tr>"
}
$body += '<td colspan=2 align=center style="background-color: #DCDCDC" border: 1 ; ><strong><font color= Black>' + "RECEIVED EMAILS STATISTICS" + '</font></strong></td></tr>'
$body += '<tr><td align="Center" style="background-color: #736F6E" width="13%" border: 1 Groove white;><p class="table" style="color: white"><b>Parameters</b></p></td>'
$body += '<td align="Center" style="background-color: #736F6E" width="10%" border: 1 Groove white;><p class="table" style="color: white"><b>Value</b></p></td>'
$body += '<tr><td align="Center" border: 1 Groove white;>' + "Total Emails Received" + "</td>"
$body += '<td align="Center" border: 1 Groove white;>' + $msgrcount + "</td></tr>"
$body += '<tr><td align="Center" border: 1 Groove white; >' + "Total Received Emails Size" + "</td>"
$body += '<td align="Center" border: 1 Groove white;>' + $rsize + "</td></tr>"
$body += '<tr><td align="Center" border: 1 Groove white; >' + "Top Receive Email size" + "</td>"
$body += '<td align="Center" border: 1 Groove white;>' + $bigrmb + "</td></tr>"
$Userarray = @()
$Measure_Receivers = Measure-Command `
{
$msgReceive | foreach {$_.Recipients -split(" ") | foreach {$Userarray += $_} | Out-Null}
$Userarray = $Userarray | group
$Receivers = $Userarray | Sort-Object Count -Descending | Select-Object -first "10"
}
$TopRec_file = ""
$TopReceiver = ""
$TopRec = "`n`n"
$body += '<td colspan=2 align=center style="background-color: #DCDCDC" border: 1 ; ><strong><font color= Black>' + "Top 10 Receivers" + '</font></strong></td></tr>'
$body += '<tr><td align="Center" style="background-color: #736F6E" width="13%" border: 1 Groove white;><p class="table" style="color: white"><b>Name</b></p></td>'
$body += '<td align="Center" style="background-color: #736F6E" width="10%" border: 1 Groove white;><p class="table" style="color: white"><b>Email Count</b></p></td>'
foreach ($User in $Receivers)
{
$rname = $user.name
$rcount = $user.count
$body += '<tr><td align="Center" border: 1 Groove white;>' + $rname + "</td>"
$body += '<td align="Center" border: 1 Groove white; >' + $rcount + "</td>"
}
}
$body += '</table><br>'
if ($Mail -eq "True")
{
$body += "Report Completed"
$body += '</p></body></html>'
##############################################################
$MailMessage.Subject = " $Subject
$MailMessage.Body = " $Body
$MailMessage.sender = " $FromAddress
$MailMessage.From = " $FromAddress
$MailMessage.To.add(" $ToAddress)
#$MailMessage.CC.add("$CCAddress")
#$MailMessage.CC.add("$CCAddress2")
$MailMessage.IsBodyHTML = $True
$smtpclient.host = " $Relay
$smtpclient.send($MailMessage)
##############################################################
}
else
{
$d = Get-Date
$name = 'Email_Statistics'
$name += $d.year.tostring()
$name += $d.month.tostring()
$name += $d.day.tostring()
$name += '.html'
Set-Content $name -value $body
}
#Script Ends
Sample HTML output
Sample Email output
No comments:
Post a Comment