locked
Email trace script for daily report RRS feed

  • Question

  • Hi Guys,

    I wanted to run this script on daily basis (in automated) that will extract previous day logs and instead of saving to local I will need to send the export to email address. 

    Get-ExchangeServer | where {$_.isHubTransportServer -eq $true -or $_.isMailboxServer -eq $true} | Get-MessageTrackingLog -Recipients:email@address.com -EventID "DELIVER" -Start "07/30/2018 00:00:00" -End "07/31/2018 00:00:00" | Select @{Name='Recipients';Expression={[string]::join(";", ($_.Recipients))}}, Sender, EventId, Timestamp, Source, MessageSubject | Export-CSV -Path C:\Users\user\Desktop\MessageTracking.csv

    I appreciate you help.


    Thanks!
    Uddan


    • Edited by Uddan Wednesday, August 1, 2018 1:26 AM missed details
    Tuesday, July 31, 2018 2:07 PM

Answers

All replies

  • help send-mailmessage -online

    Follow the examples.


    \_(ツ)_/

    Tuesday, July 31, 2018 2:17 PM
  • Hi Uddan,

    Thanks for your question.

    1. If you want to run the scripts on daily basis(in automated), you can use Task Scheduler to run the script automatically every day. About how to configure to run a PowerShell Script into Task Scheduler.

    Refer to the following link:

    https://social.technet.microsoft.com/wiki/contents/articles/38580.configure-to-run-a-powershell-script-into-task-scheduler.aspx 

    2. You want to get the log from the previous day.

    $today=Get-date
    $yesterday=$today.AddDays(-1)
    $yesterday.ToShortDateString()

    You can define a variable to get the yesterday's time and format it to match the parameter of Get-MessageTrackingLog cmdlet.

    3. You can use the command send-mailmessage to  send the export to email address.

    For example:

    Send-MailMessage -From "User01 <user01@example.com>" -To "User02 <user02@example.com>", "User03 <user03@example.com>" -Subject "Sending the Attachment" -Body "Forgot to send the attachment. Sending now." -Attachments "data.csv" -Priority High -dno onSuccess, onFailure -SmtpServer "smtp.fabrikam.com"

    Refer to the following link:

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/send-mailmessage?view=powershell-5.1

    Best Regards,

    Lee 

    • Marked as answer by Uddan Monday, August 6, 2018 12:26 AM
    Wednesday, August 1, 2018 7:34 AM