Author Posts

April 8, 2016 at 12:43 am

Hi Everyone

We are going through a rebrand at the moment and was asked by my Head of IT to produce a report from exchange 2007 to list out all the SMTP addresses for each user, fairly straight forward task.

Then he asked me to add extra statistics
For each email SMTP addresses he wanted to know how many emails were Received from external addresses, Sent to external addresses and received internally.

The reason for these extra tasks were to do a cleanup of the environment at the same time.

Below is my code that I have created, just wondering if anyone had comments on the code or better ways to do it.

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

# Output File
$strOutFile = ".\MailboxExport.csv"

#Gets all mailboxes with 
$mailboxes = get-recipient -resultsize unlimited | get-mailbox -resultsize unlimited | Select Name, forwardingaddress -ExpandProperty EmailAddresses | select name, SMTPaddress, forwardingaddress

#Gets message tracking last 30 days count for recieved SMTP items based on external email address 

$output = @()
foreach ($mailbox in $mailboxes)

#Count for Recieved emails from External SMTP Addresses
$RecievedExternal = Get-MessageTrackingLog -server mail5 -eventid "RECEIVE" -start ((get-date).adddays(-30)) -recipients $mailbox.SMTPaddress | where {$_.source -eq "SMTP" -AND $_.Recipients -eq $mailbox.SMTPaddress} | measure | select count

#Count for Sent emails to External SMTP Addresses
$Sent = Get-MessageTrackingLog -server mail5 -eventid "SEND" -start ((get-date).adddays(-30)) -Sender $mailbox.SMTPaddress | where {$_.source -eq "SMTP" -AND $_.Sender -eq $mailbox.SMTPaddress} | measure | select count

##Count for Recieved emails on internal server
$RecievedInternal = Get-MessageTrackingLog -server mail5 -eventid "RECEIVE" -start ((get-date).adddays(-30)) -Recipients $mailbox.SMTPaddress | where {$_.source -eq "STOREDRIVER" -AND $_.Recipients -eq $mailbox.SMTPaddress} | measure | select count

#Build the custom array
$output = "" | Select-Object Name, SMTPAddress, Forwardingaddress, RecievedExternal, Sent
$output.Name = $
$output.SMTPaddress = $mailbox.SMTPaddress
$output.forwardingaddress = $mailbox.forwardingaddress
$output.RecievedExternal = $RecievedExternal.count
$output.Sent = $Sent.count
$output.RecievedInternal = $RecievedInternal.count

#Export to CSV File
$output | export-csv -Append $strOutFile

April 8, 2016 at 4:19 am

Looks reasonable to me. You might get a slight performance increase, if you're running at least PowerShell v3, by slightly tweaking how you create your objects:

    $output = [pscustomobject] @{
        Name              = $mailbox.Name
        SMTPaddress       = $mailbox.SMTPaddress
        forwardingaddress = $mailbox.forwardingaddress
        RecievedExternal  = $RecievedExternal.count
        Sent              = $Sent.count
        RecievedInternal  = $RecievedInternal.Count

April 8, 2016 at 4:52 am

Thanks for taking the time to review the script Dave, will give that a try.