# Writen By : Alberto Igbinedion

# Script to track emails 

Get-Date 

Write-Host "Enter Recipients :" -ForegroundColor Yellow -NoNewline
$Recipients = Read-Host

#Write-Host "Enter Sender :" -ForegroundColor Yellow -NoNewline
#$Sender = Read-Host
#-sender $Sender

Write-Host "Enter EventID ;" -ForegroundColor Yellow -NoNewline
Write-Host "(E.g FAIL, Deliver, Send, Receive, DSN) :" -ForegroundColor White -NoNewline
$EventID = Read-Host

Write-Host "Enter StartDate :" -ForegroundColor Yellow -NoNewline
$StartDate = Read-Host

Write-Host "Enter EndDate :" -ForegroundColor Yellow -NoNewline
$EndDate = Read-Host

$MailTracked =    foreach ($server in Get-TransportServer) {Get-MessageTrackingLog -Recipients $Recipients  -eventID $EventID -Start $StartDate -End $EndDate | select Timestamp,Recipients,RecipientStatus,Sender,ClientHostname, MessageSubject  } 

$MailTracked | Out-File C:\Scripts\Output\TraCKINGlOGS.txt
 

 <#

# Track Sender Or Receiver  =================================================================================================
foreach ($server in Get-TransportServer) {Get-MessageTrackingLog -sender david.best@ons.gov.uk -eventID DELIVER -Start “16 November 2015 10:06:5” -End “17 November 2015 16:06:5” |FL| Out-File "C:\Scripts\Output\Tracking.txt" -Append }


foreach ($server in Get-TransportServer) {Get-MessageTrackingLog -Recipients michael.sherlock@Statistics.gov.uk -eventID receive -Start “10 December 2015 09:06:5” -End “10 December 2015 19:06:5” |FL| Out-File "C:\Scripts\Output\Tracking.txt" -Append }
# Track Sender Or Receiver =================================================================================================



#Find SMTP mail delivery failures # DSN, Defer, Deliver, Send, or Receive
foreach ($server in Get-TransportServer) {Get-MessageTrackingLog -EventId FAIL -Start "10 December 2015 09:06:5" -server $server.name | 
    where {$_.recipients -like '*@*' -and $_.recipients -notlike '*@local.com' -and $_.recipients -notlike 'IMCEAEX*'} | ft EventId,Source,Sender,Recipients,ClientHostname -wrap -autosize | Out-File "C:\Scripts\Output\Tracking.txt" -Append }


#Find NDR 5.4.6 routing loops in the last 3 day from all transport servers
foreach ($server in Get-TransportServer) {Get-MessageTrackingLog -resultsize unlimited -EventId FAIL -Start (Get-Date).AddDays(-3) -server $server.name | 
    where {$_.RecipientStatus -like '*5.4.6*'} | ft Timestamp,Recipients,RecipientStatus,Sender -wrap -autosize | Out-File "C:\Scripts\Output\Tracking.txt" -Append }

#Find logs for failed messages
foreach ($server in Get-TransportServer) {Get-MessageTrackingLog  -EventId FAIL -Start "10 December 2015 09:06:5" -server $server.name | 
    ft Timestamp,Recipients,RecipientStatus,Sender -wrap -autosize | Out-File "C:\Scripts\Output\Tracking.txt" -Append }


#Find logs for messages from the last minute
foreach ($server in Get-TransportServer) {Get-MessageTrackingLog -start (Get-Date).AddMinutes(-1)  -server $server.name | 
    ft Timestamp,Sender,RelatedRecipientAddress,Recipients,RecipientStatus -wrap -autosize | Out-File "C:\Scripts\Output\Tracking.txt" -Append }

#Find Exchange logs for messages that failed to deliver
foreach ($server in Get-TransportServer) {Get-MessageTrackingLog  -resultsize unlimited -Start "10 December 2015 09:06:5" -server $Server.Name | 
    where {$_.eventId -eq 'Fail'} | ft * -wrap -autosize | Out-File "C:\Scripts\Output\Tracking.txt" -Append }


#Find messages where delivery failed 
Get-MessageTrackingLog -EventId FAIL -Start "10 December 2015 09:06:5" -server server01 | ft TimeStamp,Source,EventID,Recipients,Sender,RecipientStatus -wrap -autosize 

#Find Exchange Message Tracking messages from a particular client IP
foreach ($server in Get-TransportServer) {Get-MessageTrackingLog -resultsize unlimited -start (Get-Date).AddMinutes(-15) -server $server.name | 
    where {$_.ClientIp -eq 'np3rvxmimgw023'} | ft * -wrap -autosize | Out-File "C:\Scripts\Output\Tracking.txt" -Append }

#Check Exchange Message Tracking Logs based on message ID
Get-MessageTrackingLog -MessageId 'FBE264977E286848971C4C15BDD7F5FE439245C5EF@mx.company.com.au' -Start "10 December 2015 09:06:5" -End "10 December 2015 18:06:5" -server server01

#Check Exchange Message Tracking Logs based on mail subject
foreach ($server in Get-TransportServer) {Get-MessageTrackingLog -resultsize unlimited -MessageSubject "test" -Start "10 December 2015 09:06:5" -End "10 December 2015 18:06:5" -server $server.name | 
    ft * -wrap -autosize | Out-File "C:\Scripts\Output\Tracking.txt" -Append}




    #>