none
Message Tracking Report

    Question

  • I am trying to create a powershell script to generate message tracking logs for give set of user, but fail to get output. Below is the script that I wrote

    $file = import-csv C:\Users\ashxadm\Desktop\input.csv
    [string]$sdate = Read-Host "Start Date MM/DD/YYYY HH:MM:SS "
    [string]$edate = Read-Host "End Date MM/DD/YYYY HH:MM:SS "
    $Mailserver = Read-Host "Provide Serve Name"

    foreach ($Record in $file)
    {
     $ErrorActionPreference = "SilentlyContinue"
     $sender = $Record.sender
     $exdata = Get-MessageTrackingLog -Server "$MailServer" -Sender "$sender" -Start "$sdate" -End "$edate" -EventId SEND -ResultSize Unlimited |Select-object TimeStamp, Sender, Recipients, MessageSubject|ft -AutoSize -Wrap 
     Out-File C:\Users\ashxadm\Desktop\Report.csv -Append
    }

    The logic that I have used here is Auditor dumps all the smtp addresses in the input.csv file
    I will then import the file and for every smtp in that file I will execute commad Get-MessageTrackingLog
    The output of this will then be stored into the output file Report.csv

    Please help to debug this code

    Monday, August 26, 2013 3:11 PM

Answers

  • Hi Ankush,

    It will be more informative for us if you had post  the error   , which you are getting.

    However , please try below modified script's portion  , it should work .

    foreach($Record in $file)
    { 
    
    $ErrorActionPreference = "SilentlyContinue" 
    $sender = $Record.sender 
    Get-MessageTrackingLog -Server "$MailServer" -Sender "$sender" -Start "$sdate" -End "$edate" -EventId "SEND" -ResultSize Unlimited |ft TimeStamp, Sender, Recipients, MessageSubject -AutoSize -Wrap | Out-File C:\Users\ashxadm\Desktop\Report.csv -Append
    
    }

    Let us know if you face any error .


    • Edited by Tarique Noorain Monday, August 26, 2013 6:28 PM some changes
    • Marked as answer by Ankush Shah Thursday, August 29, 2013 3:17 AM
    Monday, August 26, 2013 6:27 PM

All replies

  • Hi Ankush,

    It will be more informative for us if you had post  the error   , which you are getting.

    However , please try below modified script's portion  , it should work .

    foreach($Record in $file)
    { 
    
    $ErrorActionPreference = "SilentlyContinue" 
    $sender = $Record.sender 
    Get-MessageTrackingLog -Server "$MailServer" -Sender "$sender" -Start "$sdate" -End "$edate" -EventId "SEND" -ResultSize Unlimited |ft TimeStamp, Sender, Recipients, MessageSubject -AutoSize -Wrap | Out-File C:\Users\ashxadm\Desktop\Report.csv -Append
    
    }

    Let us know if you face any error .


    • Edited by Tarique Noorain Monday, August 26, 2013 6:28 PM some changes
    • Marked as answer by Ankush Shah Thursday, August 29, 2013 3:17 AM
    Monday, August 26, 2013 6:27 PM
  • Something to keep in mind is that the DELIVER eventID is what you see when most email is sent to an internal person, and SEND will commonly return items sent externally.  Since you are searching only for SEND, any DELIVERs will be ignored.  If you need info about both of these, you will need additional logic to handle the two ...
    Monday, August 26, 2013 8:41 PM
  • Out-File needs some input object which is missing in your code. So the example written by Tarrique should workl for you.

    However, this would work just in case you have only one hub transport server installed in your organization. Much more general code:

    function Get-EmailTraffic()
    {
        Param(
            [string]$Identity,
            [string]$Start,
            [string]$End
        )
        
        $result = @()
        
        # Convert parameter to e-mail address
        $MailAddr = (Get-Recipient -Identity $Identity -ErrorAction "Stop").PrimarySmtpAddress

        try {
            $objEnd = Get-Date $End
        }
        catch [Exception] {
            $objEnd = Get-Date    # TODAY
        }
        try {
            $objStart = Get-Date $Start
        }
        catch [Exception] {
            $objStart = $objEnd.AddMonths(-1)
        }
        $End = $objEnd.ToString("yyyy-MM-dd 00:00:00")
        $Start = $objStart.ToString("yyyy-MM-dd 00:00:00")
        
        # Scan for all the SMTP servers in organization (= hub transport servers)
        $hts = Get-TransportServer -ErrorAction "Stop"
        $msg = $hts | ForEach-Object {
            # Get received messages
            $result += Get-MessageTrackingLog -Server $_.Name -ResultSize unlimited -Start $Start -End $End `
                -Sender $MailAddr -EventId "RECEIVE"
            # Get sent messages
            $result += Get-MessageTrackingLog -Server $_.Name -ResultSize unlimited -Start $Start -End $End `
                -Recipients:$MailAddr -EventId "DELIVER"
        }
        return $result
    }

    Usage is quite obvious:

    $logs = Get-EmailTraffic -Identity addr@domain.com -Start "2013-08-01" -End "2013-08-05"
    $logs | Sort-Object TimeStamp | Export-Csv _msg.csv -UseCulture -Encoding utf8

    If you have Excel installed on the box, you can open the file in it executing start _msg.csv, for example.

    Tuesday, August 27, 2013 7:19 AM
  • Thanks Tarique,the correction you suggested worked and I am getting the desired output.

    Could you also suggest some logic as to how do I covert this output to a html formatted output ? When I am using ConvertTo-HTML cmdlet I get a blank file and when I try to import this csv file and then convert it to html formatted output file I am getting scrap values.

    To move onto my objective on this requirement, I am trying to push the output data to an html file and sending it as a email (data is to be pushed into HTML body of the mail). I would be really thankful if you could share some logic that could help me to achive this.

    Thanks Again :)

    Thursday, August 29, 2013 3:17 AM
  • Hi Camlost,

    Thanks for the response, but I haven't tried this as Tarique answer solved my initial problem.
    I will try to check your script in my environment and let you know the result soon.

    Also as an extension to this script can you suggest some logic using which I can pass the output to an HTML file and sending that via email (I am trying to push the file output body of the email no attachments)

    Thanks Again for sharing your logic.

    Thursday, August 29, 2013 3:24 AM
  • Hi,

    To convert output in HTML format , you can do like this:

    Get-MessageTrackingLog -Server "$MailServer" -Sender "$sender" -Start "$sdate" -End "$edate" -EventId "SEND" -ResultSize Unlimited |convertTo-html -property TimeStamp, Sender, Recipients, MessageSubject > c:\Output.htm

    But there is Problem with ConvertTo-HTML cmdlet as  there is no -Append parameter with it , so you can't accumulate whole output in one file.

    And I don't think we can send this output via mail ( May be possible , but I don't know how to do)


    If you found my post helpful, please give it a Helpful vote. If it answered your question, remember to mark it as an Answer. ---------- MCITP - Exchange 2010 | MCITP - Windows Server 2008 R2

    Friday, August 30, 2013 4:03 AM