Link to home
Start Free TrialLog in
Avatar of Navishkar Sadheo
Navishkar SadheoFlag for South Africa

asked on

Incoming and outgoing email statistics

Dear Experts

I need some help please. I need to gather incoming and outgoing email statistics per day for an Exchange 2010 server? Any advice?

The most important thing is the total size of all incoming and outgoing per daily basis.
Avatar of Shreedhar Ette
Shreedhar Ette
Flag of India image

There is a script on Microsoft technet website. You can try using it.

https://gallery.technet.microsoft.com/scriptcenter/bb94b422-eb9e-4c53-a454-f7da6ddfb5d6
Or you can use this one which you can customize it your self as email report:

#Add Exchange Snapin#
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin

#Get Date Variables.  You can run this script for any time period by adjusting the .AddDays(-x) value.
## If you change this report to run for a weeks period you will want to change the email and table titles below in the html section to match.
$subjectDate = get-date -uformat %Y/%m/%d
$start = ((get-date).Adddays(-1))
$end = get-date

#Get Transport Logs.  I exclude any email sent from MicrosoftExchange*@domain.com, I didn't want to count system generated emails in this report.
$logs = Get-TransportServer | Get-MessageTrackingLog -start $start -end $end -resultsize unlimited | Where-Object {$_.Sender -notlike 'MicrosoftExchange*@domain.com'} | Select-Object EventId, ConnectorId, Source, Sender, Recipients, RecipientCount, TotalBytes

#It's important to know that a receive EventID will exist for every email submitted to your exchange server from any source.
#Separate Receive and Send.  We use send later on to break out the email count from internal to external.
$receive = $logs | Where-Object {$_.EventId -eq 'RECEIVE'} | Select-Object EventId, ConnectorId, Source, Sender, Recipients, RecipientCount, TotalBytes
$send = $logs | Where-Object {$_.EventId -eq 'SEND'} | Select-Object EventId, ConnectorId, Source, Sender, Recipients, RecipientCount, TotalBytes

#Seperate Internal and External based on the sender of submitted messages.
## If you have multiple domains like I did, you need to add them to the where object command here
## Keep in mind that -notlike will use -AND and -like will use -OR for this.
### Example {$_.Sender -notlike '*@domain.com' -AND $_.Sender -notlike '*@your2nddomain.com'}
### Example {$_.Sender -like '*@domain.com' -OR $_.Sender -like '*@your2nddomain.com'}
$external = $receive | Where-Object {$_.Sender -notlike '*@domain.com'} | Select-Object EventId, ConnectorId, Source, Sender, Recipients, RecipientCount, TotalBytes
$internal = $receive | Where-Object {$_.Sender -like '*@domain.com'} | Select-Object EventId, ConnectorId, Source, Sender, Recipients, RecipientCount, TotalBytes

#get sums for recipientcount and totalbytes
$recSum = $receive | Measure-Object RecipientCount -sum
$recSumSize = $receive | Measure-Object TotalBytes -sum
$extRecSum = $external | Measure-Object RecipientCount -sum
$extRecSize = $external | Measure-Object TotalBytes -sum
$intRecSum = $internal | Measure-Object RecipientCount -sum
$intRecSize = $internal | Measure-Object TotalBytes -sum
$sendSum = $send | Measure-Object RecipientCount -sum
$sendSumSize = $send | Measure-Object TotalBytes -sum

#Divide by 1GB and round down to the first decimal point
$rss = [math]::Round($recSumSize.Sum / 1GB, 1)
$ers = [math]::Round($extRecSize.Sum / 1GB, 1)
$irs = [math]::Round($intRecSize.Sum / 1GB, 1)
$sss = [math]::Round($sendSumSize.Sum / 1GB, 1)

#Reduce variables to single value, resolves issue where email count showed as 'Microsoft.PowerShell.Commands.GenericMeasureInfo.sum' in html table
## There is probably a better way to achive this, this is the way I found to get data to show in my reports
$recSum = $recSum.sum
$extRecSum = $extRecSum.sum
$intRecSum = $intRecSum.sum
$sendSumSize = $sendSumSize.sum
$sendSum = $sendSum.sum
$sendSumSize = $sendSumSize.sum

#Create HTML
##This code includes CSS formatting for the tables.  I did not like the format of the table without CSS to control font size and colors.
##The varuables created above are placed into the table below.  It's not as clear to see since surrounding it with "" removed the syntax highlighting.
$html = "<html>
<head>
<title>Daily Email Metrics</title>
<!-- CSS goes in the document HEAD or added to your external stylesheet -->
<style type='text/css'>
table.imagetable {
	font-family: verdana,arial,sans-serif;
	font-size:10px;
	color:#333333;
	border-width: 1px;
	border-color: #999999;
	border-collapse: collapse;
}
table.imagetable th {
	background:#CCCCCC;
	border-width: 1px;
	padding: 6px;
	border-style: solid;
	border-color: #999999;
}
table.imagetable td {
	background:#FFFFFF;
	border-width: 1px;
	padding: 6px;
	border-style: solid;
	border-color: #999999;
}
</style>
<head>
<body><center><h3>Daily Email Metrics - $subjectDate</h3></center>
<table class='imagetable' align=center>
<tr><th>Data</th><th>Value</th></tr>
<tr><td>Total Number of Emails</td><td>$recSum</td></tr>
<tr><td>Total Size of Emails (gb)</td><td>$rss</td></tr>
<tr><td>Total Number of Emails from External Senders</td><td>$extRecSum</td></tr>
<tr><td>Size of Emails from External Senders (gb)</td><td>$ers</td></tr>
<tr><td>Number of Emails from Internal Senders</td><td>$intRecSum</td></tr>
<tr><td>Size of Emails from Internal Senders(gb)</td><td>$irs</td></tr>
<tr><td>Number of Emails from Internal to External</td><td>$sendSum</td></tr>
<tr><td>Size of Emails from Internal to External (gb)</td><td>$sss</td></tr>
</table>
</body>
</html>"

#Send Email
#This email send requires the server running the script to be allowed in the unauthenticated relay of your exchange server.
$from = New-Object System.Net.Mail.MailAddress "sender@domain.com" 
$to = New-Object System.Net.Mail.MailAddress "Admin@domain.com" 
$msg = new-object system.Net.Mail.MailMessage $from, $to
$msg.IsBodyHTML = $true
$msg.Subject = "Daily Email Metrics - $subjectDate"
$msg.body = $html
$smtpServer = 'RELAY.domain.com' #IP of SMTP Server.
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($msg);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Akhater
Akhater
Flag of Lebanon image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial