Daily Sent Items Report with PowerShell

elow is a PowerShell script I wrote recently for a customer that wanted daily emailed reports of all messages sent from a specified email address.

The script is called Generate-DailySendReports.ps1 and takes command line parameters as input.

The script uses command-line parameters as input:

  • SenderSMTP (Required) – This is the sender smtp address you want to search the logs for.
  • Days (optional) – This is how many days back in time you want to search. The default is 1 day which will cause the script to search back n-1 days.
  • SMTPServer (optional) – This is the SMTP server you want to use to send the resulting email. By default, it will use the local host the script is running against. So if you’re not running this script from an Exchange server, you’ll want to supply this value or the script will fail to send the report.
  • ReportRecipient (Required) – What email address(es) do you want to send the report to. Separate addresses by a comma without spaces (i.e. user1@contoso.com,user2@fabrikam.com)
  • ReportSender (optional) – What email address do you want the report sent from? By default it will send from Reports@<logged-in-user-domain>
  • TransportServer (optional) – What transport server(s) do you want to search through? By default, it will only search the local server the script is running on. So, if you’re not running this script from an Exchange server, you’ll want to specify which server’s logs to search.

When the script runs, if it finds results it generates a CSV file of selected fields from the transport logs; specifically, it includes ClientIP, ServerIP, ConnectorID, MessageID, Recipients (expands this list if multiple) and MessageSubject. It then attaches this CSV file to a new email message that it sends to the email addresses you provide in the ReportRecipient parameter.

Copy the script below and save to a file named Generate-DailySendReports.ps1

# ===================================================

#

# This PowerShell script does the following:

# 1. Generates a CSV report for Messages Sent

# from a specified email address going back

# a specified number of days.

# 3. Sends an email with the resulting CSV file

# as an attachment

#

# Written by: David Smith

# Last modified: 11/21/2012

# Notes:

#

# ===================================================

 

#——-Establish variables

[CmdletBinding()]

Param(

[Parameter(Mandatory=$True)]

[string]$SenderSMTP,

 

[Parameter(Mandatory=$false)]

[int]$Days=1,

 

[Parameter(Mandatory=$false)]

[string]$SMTPServer=$env:COMPUTERNAME,

 

[Parameter(Mandatory=$true)]

[array]$ReportRecipient,

 

[Parameter(Mandatory=$false)]

[string]$ReportSender=(“Reports@”
+ $env:USERDNSDOMAIN
),


 

[Parameter(Mandatory=$false)]

[string]$TransportServer=$env:COMPUTERNAME

)

 

 

Add-PSSnapin
Microsoft.Exchange.Management.PowerShell.E2010
-ErrorAction
SilentlyContinue

$Date
= (get-date).AddDays($Days)

$ReportSubject
= ($SenderSMTP Sent Items Report – “
+ $date.ToShortDateString() +
” to “
+
[string](Get-Date).ToShortDateString())

 

#——-Generage the report results

$SentMessages
=
Get-MessageTrackingLog
-Start
$date
-Sender
$SenderSMTP
-EventId
SEND
-ResultSize
Unlimited
-Server
$TransportServer

$CountSentMessages
= ($SentMessages
|
Measure-Object).Count

 

#——-Determine the Body of the message and attachment

if ($SentMessages
-eq
$null)

{$ReportMessage
=
“0 Messages sent from $SenderToCheck since $date

Send-MailMessage
-From
$ReportSender
-To
$ReportRecipient
-Subject
$ReportSubject
-Body
$ReportMessage
-SmtpServer
$SMTPServer}

Else

{$ReportMessage
=
$CountSentMessages Messages Sent from $SenderSMTP since $date

$SentMessages
|
Select
TimeStamp, ClientIP, ServerIP, ConnectorID, MessageID, {$_.Recipients},
MessageSubject
|
Export-Csv
-NoTypeInformation
.SentItemsReport.csv

Send-MailMessage
-From
$ReportSender
-To
$ReportRecipient
-Subject
$ReportSubject
-Body
$ReportMessage
-Attachment
.SentItemsReport.csv
-SmtpServer
$SMTPServer}

 

 

 


 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s