Message Tracking results via email

Hi Readers,

I am sharing Message tracking script today that I had made for monitoring Team in our enviornment.

This script tracks email based on sender & send the csv report in an email.

you can define the number of days for which you want to extract the tracking data

—————————————————————————————————————————————————

##############################################################

#               Author: Vikas Sukhija
#               Date:- 02/22/2013
# Description:- This script will Track the logs for monitoring team
#                for analysis of most troublesome CI’s
##############################################################             

#——————-Add Exchange Shell—————————————–

If ((Get-PSSnapin | where {$_.Name -match “Exchange.Management”}) -eq $null)
{
 Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin
}

#—————-Use Date Functions ——————————————-

$start = (get-date).adddays(-1)
$end  = get-date

# format date

$date = get-date -format d
# replace \ by –

$date = $date.ToString().Replace(“/”, “-“)

$output =  “C:\Scripts” + “\” + “BMCCITracking” + “\” + “MessageTracking” + “_” + “$date” + “CI.csv”

#————————–Define email addreseses——————————

$sender1 = “xxxxxxxxx@aaaaaaaaaaaaaaaaaaa.com
$sender2 = “yyyyyyyyyyyyyyyyyyyyyy@aaaaaaaaaaaaaaaaaaaaaaaaaa.com
$reportemail = “zzzzzzzzzzzzzzzzz@aaaaaaaaaa.com

#—————-Start Message Tracking—————————————-

Get-transportserver |
Get-MessageTrackingLog -Start $start -End $end  -resultsize unlimited |
where { ((($_.sender -like $sender1) -or ($_.sender -like $sender2)) -and ($_.EventId -like “RECEIVE”))} |
select Timestamp,clientip,ServerIp,ServerHostname,sender, EventId,MessageSubject,@{Name=”Recipents”;Expression={$_.recipients}} |
export-csv $output

#——————————Send email ————————————-

$message = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient(“smtpserver”)
$message.From = “xxxxxx@xxxxxx.com
$message.To.Add($reportemail)
$file = $output
$att = new-object Net.Mail.Attachment($file)
$message.IsBodyHtml = $False
$message.Subject = “Monthly Tracking Monitoring CI”
$message.Attachments.Add($att)
$smtp.Send($message)

##########################################################

You can modify the tracking code to include recipients as well.

Regards

Sukhija Vikas

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