Wednesday, February 27, 2013

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------------------------------
#----------------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

No comments:

Post a Comment