A powershell script that sends email about sent and received message counts on Exchange 2010

One of my customer ask me that is there any way to obtain sent and received message counts on Exchange 2010 for specific timelines.With following script, you can get all transport servers on your organization, read sent and received message counts for last 24 hour period , save this to the local disk, and email to the specific user with this attachment. Nice right? : )

Now I’m building a windows form that you will be able to choose specific hub transport servers and timelines.

 


$Hubs = @(Get-TransportServer)

$date = ( get-date ).ToString(‘MMddyyyy’)

$Out = c:tempGunluk_Mesaj_Adet_Raporu.txt

$Sent = 0
$Sent1 = 0
$Rcvd = 0
$Rcvd1 = 0

$EDate = get-date -uFormat ‘%D %r’

$SDate = (get-date (get-date).AddDays(-1) -uformat ‘%D %r’)

foreach ($Hub in $Hubs)
{
      #count Sent messages
      $msg = @(get-messagetrackinglog -Server $Hub -EventID Send -Start $SDate -End $EDate -ResultSize Unlimited)
      $Sent1 = $msg.Count
      #add to total for all Hub servers
      $Sent = $Sent + $Sent1
     
      #Count received messages
      $msg2 = @(get-messagetrackinglog -Server $Hub -EventID Receive -Start $SDate -End $EDate -ResultSize Unlimited)
      $Rcvd1 = $msg2.Count
      #add to total for all hub servers
      $Rcvd = $Rcvd + $Rcvd1
}     

out-File $Out -InputObject $date
$inp = Son 24 Saat Icerisinde Tum Hub Sunucular Tarafindan Gonderilen Mailler  + $Sent + adet
out-File $Out -InputObject $inp -Append
$inp2 = Son 24 Saat Icerisinde Tum Hub Sunucular Tarafindan Alinan Mailler  + $Rcvd + adet
out-File $Out -InputObject $inp2 -Append

Function Send-Mail
{
$mail = New-Object Net.Mail.MailMessage
#Specify what email address to send the report from
$mail.From  = exchange2010@anilerduran.com
#Specify the recipient’s email address
$mail.To.Add(admin@anilerduran.com)
$mail.Subject = Gunluk Mail Analiz Scripti Sonuclari
$mail.Body = Gunluk Mail Analiz Scripti Sonuclariniz Ektedir.
$Attachment = New-Object Net.Mail.Attachment($Out)
$mail.Attachments.Add($Attachment)
$smtp = New-Object Net.Mail.SmtpClient
#Specify you SMTP Server
$smtp.Host = smtpservername
$smtp.Port = 25
$smtp.Send($mail)
}
Send-Mail

Leave a Reply

Your email address will not be published. Required fields are marked *

9 + 1 =