Description

The purpose of this script is to count the number of e-mails that are sent and received in your Exchange environment every day and separated by hour.

It is a very basic script that doesn't count undelivered e-mails for example (although this could be easily checked) and that might be complicated to understand at first (due to the lack of comments as well). However, I believe that if you run it once and then look at the code, you will understand it straight away.

 

NOTE:

  1. I have included $_.MessageSubject -ne "Folder Content" so that messages regarding Public Folder replication are not counted.
  2. For Exchange 2013 environments, you might also want to exclude e-mails sent from inboundproxy@inboundproxy.com, MailDeliveryProbe@MailDeliveryProbe.com and HealthMailbox(...)@yourdomain.com, which are used by Managed Availability. More information here: http://letsexchange.blogspot.co.uk/2013/09/exchange-2013-e-mail-traffic-from.html.

 

When you run it, you will get an output similar to this:

Sent
DayOfWeek,Date,00:00,01:00,02:00,03:00,04:00,05:00,06:00,07:00,08:00,09:00,10:00,11:00,12:00,13:00,14:00,15:00,16:00,17:00,18:00,19:00,20:00,21:00,22:00,23:00
Monday,07/06/2010,153,184,650,31,146,59,475,335,974,2501,4319,7375,6854,4932,4109,4916,3667,2319,919,608,488,539,630,5105
Tuesday,08/06/2010,180,173,647,38,172,116,240,424,990,3082,4055,4479,3517,3098,3056,3455,3223,1978,1805,920,575,572,415,329

Received
DayOfWeek,Date,00:00,01:00,02:00,03:00,04:00,05:00,06:00,07:00,08:00,09:00,10:00,11:00,12:00,13:00,14:00,15:00,16:00,17:00,18:00,19:00,20:00,21:00,22:00,23:00
Monday,07/06/2010,433,481,822,185,1424,254,1886,707,1488,3342,4835,5636,5031,4358,4695,5061,5000,3388,1622,1238,1027,1015,1027,1343
Tuesday,08/06/2010,665,570,872,242,1678,390,1082,911,1583,4035,5499,6225,5359,4685,4926,5090,4700,3064,2098,1542,1062,1189,935,867

Once you put this data in Excel, you can produce some interesting graphs.

 

Script

PowerShell
Edit|Remove
# Script:    TotalEmailsSentReceivedPerHour.ps1 
# Purpose:    Get the number of e-mails sent and received per hour 
# Author:    Nuno Mota 
# Date:        October 2010 
 
$From = Get-Date "01/11/2011" 
$To = $From.AddHours(1) 
 
[Int64] $intSent = 0 
[Int64] $intRec = 0 
[String] $strSent = $strTotalSent = $null 
[String] $strRec = $strTotalRec = $null 
 
 
Do 
{ 
    If ($From.Hour -eq "0") { 
        $strTotalSent +"$strSent`n" 
        $strTotalRec +"$strRec`n" 
 
        $strSent = "$($From.DayOfWeek),$($From.ToShortDateString())," 
        $strRec = "$($From.DayOfWeek),$($From.ToShortDateString())," 
         
        Write-Host "Searching $From" 
    } 
     
     
    # It is faster to search the Transport Logs this way then by doing a Get-TransportServer | Get-MessageTrackingLog -ResultSize Unlimited -Start $From -End $To and then checking if it is a sent or received e-mail  
    # Sent E-mails 
    $intSent = (Get-TransportService | Get-MessageTrackingLog -ResultSize Unlimited -EventId RECEIVE -Start $From -End $To | Where {$_.Source -eq "STOREDRIVER" -and $_.MessageSubject -ne "Folder Content" -and $_.Sender -notlike "HealthMailbox*" -and $_.Sender -notlike "maildeliveryprobe*" -and $_.Sender -notlike "inboundproxy*" -and $_.Recipients -notmatch "journal@domain.com"}).Count 
    $strSent +"$intSent," 
     
    # Received E-mails 
    Get-TransportService | Get-MessageTrackingLog -ResultSize Unlimited -EventId DELIVER -Start $From -End $To | Where {$_.MessageSubject -ne "Folder Content" -and $_.Sender -notlike "HealthMailbox*" -and $_.Sender -notlike "maildeliveryprobe*" -and $_.Sender -notlike "inboundproxy*" -and $_.Recipients -notmatch "journal@domain.com"| ForEach {$intRec +$_.RecipientCount} 
    $strRec +"$intRec," 
    $intRec = 0 
 
    $From = $From.AddHours(1) 
    $To = $From.AddHours(1) 
} 
While ($To -lt (Get-Date)) 
 
# Print all the results 
$strTotalSent +"$strSent`n" 
$strTotalRec +"$strRec`n" 
 
Write-Host "`nSent" -ForegroundColor Yellow 
Write-Host "DayOfWeek,Date,00:00,01:00,02:00,03:00,04:00,05:00,06:00,07:00,08:00,09:00,10:00,11:00,12:00,13:00,14:00,15:00,16:00,17:00,18:00,19:00,20:00,21:00,22:00,23:00" -ForegroundColor Yellow 
$strTotalSent 
 
Write-Host "`nReceived" -ForegroundColor Yellow 
Write-Host "DayOfWeek,Date,00:00,01:00,02:00,03:00,04:00,05:00,06:00,07:00,08:00,09:00,10:00,11:00,12:00,13:00,14:00,15:00,16:00,17:00,18:00,19:00,20:00,21:00,22:00,23:00" -ForegroundColor Yellow 
$strTotalRec