Powershell script to report on total send/received e-mails in Exchange

 

Update: 2017-08-16 – I have published a new version of this script here

 

This is a simple PowerShell script that pulls all of the send/received e-mails from the Message Tracking log in Exchange 2010 and counts the unique header IDs.

It’s fairly accurate but I don’t think it’s 100% bang on. My testing showed the numbers generated to be off by about 10%.

This script was built for generic mailboxes but should work on individual mailboxes as well.

 

####################################
# Exchange 2010 send/receive weekly report generator
# Created by: Eric Schewe
# Created on: 2014-10-02
#
####################################
# Summary:
#   This script will count the unique message IDs send and recieved by a mailbox
#   on a weekly basis and e-mail a report to the designated recipients. We are
#   specifically using this script to track mailflow to and from a generic account
#   in our enviroment.
#
#   This script is meant to be run on a Monday so it can gather the previous weeks
#   e-mails (Monday - Sunday).
#
####################################
# Instructions:
#   1. Set $mailbox to the generic account you want stats from
#   2. Set $emailFrom to the e-mail address that the report will be sent from
#   3. Set $emailTo to the people who will receive the report
#   4. Set $smtpServer to a SMTP server you can send e-mail from
#   
####################################

#Powershell Garbage
$nl = [Environment]::NewLine

#Mailbox to gather stats on
$mailbox=""

#Get todays date twice
$startDate=Get-Date
$endDate=Get-Date

#Subtract 1 day from todays date (report ending day) and 7 days from todays date (report starting day)
$startDateFormatted=$startDate.AddDays(-7).ToShortDateString()
$endDateFormatted=$endDate.AddDays(-1).ToShortDateString()


#Who to send the e-mail report to.
#Multiple e-mail addresses should be in this format "<[email protected]>, <[email protected]>"
$emailFrom = "[email protected]"
$emailTo = ""
$subject = "Weekly e-mail report for $mailbox for $startDateFormatted - $endDateFormatted"
$smtpServer = ""


# Sent e-mails
$sendCount = Get-TransportServer | Get-MessageTrackingLog -Start "$startDateFormatted 00:00:00" -End "$endDateFormatted 23:59:59" -Sender $mailbox -resultsize unlimited | select-object -unique MessageId

# Received e-mails - This works but not on generic accounts
$receiveCount = Get-TransportServer | Get-MessageTrackingLog -Start "$startDateFormatted 00:00:00" -End "$endDateFormatted 23:59:59" -Recipients $mailbox -resultsize unlimited | select-object -unique MessageId

$sendCountString = $sendCount.count
$receiveCountString = $receiveCount.count

$body = "Mailbox stats for: $mailbox $nl
Report date range: $startDateFormatted 00:00:00 - $endDateFormatted 23:59:59 $nl
Total e-mails sent: $sendCountString $nl
Total e-mails received: $receiveCountString"

$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)

I have a scheduled task configured on one of our Exchange servers that runs this every Monday morning and provide me the stats on a generic mailbox for the previous week (Monday – Sunday).

KB2917508 (Update Rollup 5 for Exchange Server 2010 Service Pack 3) appears to hang during installation

Last updated: [last-modified]

Recently I had to install KB2917508 on one of our existing Exchange 2010 SP3 servers. This Rollup had been successfully installed on our 4 other Exchange servers but for some odd reason this server it would appear to hang during the install progress. The progress bar would stop just short of 50% and the update would just sit there

To get out of the update installer you’d have to reboot the server and then manually re-enable all of the Exchange services that were supposed to start on boot and then reboot one more time. The Rollup would of course fail with no useful information appearing in the Event viewer on the server.

The issue ended up being very similar to this: http://support.microsoft.com/kb/2784788 except that we weren’t getting an error message OR being prompted to find the missing source files.

Following the instructions in http://support.microsoft.com/kb/2784788 I re-ran the update as follows:

C:\Temp> Exchange2010-KB2917508-x64-en.msp /lxv C:\Temp\Rollup.log

I then let the patch run until the point where it hung and checked the log file and found this:

MSI (s) (EC:C4) [19:35:14:055]: SOURCEMGMT: Now checking product {4934D1EA-BE46-48B1-8847-F1AF20E892C1}
MSI (s) (EC:C4) [19:35:14:055]: SOURCEMGMT: Media is enabled for product.
MSI (s) (EC:C4) [19:35:14:055]: SOURCEMGMT: Attempting to use LastUsedSource from source list.
MSI (s) (EC:C4) [19:35:14:055]: SOURCEMGMT: Trying source \\<SERVER>\<SHARE>\exsp3\.

Turns out the patch was looking for the source files for Exchange 2010 SP3 which we’d previously installed it from a CIFS share in our organization and have since deleted the source files.

I re-created the share and folders, downloaded a fresh copy of Exchange 2010 Sp3, extracted it into the share and re-ran the patch and everything went through properly.

Thanks to Farkhad Makhmudov for his two blog posts which lead me down this path of troubleshooting: