Solved

Script to find Daily receive emails

Posted on 2013-12-16
2
511 Views
Last Modified: 2014-01-11
Hello,

I want to ran an script on schedule basis to get one recipient's daily's receive email then send to his email address.  

Get-MessageTrackingLog -Start (Get-Date).adddays(-1) -ResultSize unlimited -Recipients user1@contoso.com | select-object Sender, MessageSubject, Timestamp | ConvertTo-Html | Out-File E:\Report\report.html

I want to above cmdlet run on the task schedule on end of the day and send email to recipient.
0
Comment
Question by:Kanchana_Silva
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 3

Expert Comment

by:jrlingam
ID: 39723270
Hi

Please try the below steps and let me know the results.

1. Store the above cmdlet in a .ps1 file.
2. Open notepad and type the below command (without wordwrap)

PowerShell.exe -PSConsoleFile "<exchange installation drive>\Exchsrvr\Bin\ExShell.psc1" -Command ".\ 'full path of the ps1 file'"

This command will invoke the exchange management shell first and then run the .ps1 file.

eg:
PowerShell.exe -PSConsoleFile "D:\Microsoft\Exchsrvr\Bin\ExShell.psc1" -Command ".\ 'D:\test.ps1'"

Save the notepad as a batch file (.bat)

3. Now open task scheduler and select create basic task.
Provide a name and proceed next --> choose daily and click next --> select the time you want the task to be executed --> in action select 'start a program' and click next --> in the program/script box, browse and provide the .bat file that was saved and click finish.

Check whether the task is running as scheduled.

To send this report as an email, you will have to write a separate script and schedule it to run after this task completes and then add the attachment to it.

Kindly revert back.
0
 
LVL 19

Accepted Solution

by:
suriyaehnop earned 500 total points
ID: 39724463
Get-MessageTrackingLog -Start (Get-Date).adddays(-1) -ResultSize unlimited -Recipients user1@contoso.com | select-object Sender, MessageSubject, Timestamp | ConvertTo-Html | Out-File E:\Report\report.html 


$SMTPserver = 'send.stmp.com'
$fileattachment = 'E:\Report\report.html'
$from = 'admin@contoso.com'
$to = 'user1@contoso.com'
$cc = 'user2@contoso.com'
$subject = 'New Joiner - STB'
$MessageBody = "Hi" + "`n`n"+'The report is created, details as per attachment'+"`n`n`n"+"Regards"+"`n"+"Admin"
$msg = New-Object Net.Mail.MailMessage($from,$to,$subject,$MessageBody)
$mail = New-Object Net.Mail.SmtpClient($SMTPserver)
$msg.cc.add($cc)
$attachment = New-Object Net.Mail.Attachment -ArgumentList ($fileattachment)
$msg.attachments.add($attachment)
$mail.send($msg)

Open in new window


Saved this code and follow instruction from jrlingam whereby you have to create a batch file.

The script will run and produce the report, and attach it to email.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Changing "From" field - Exchange 2013 5 54
exchange 16 47
outlook, autodiscover 1 57
Unable to create lists in Exchange 2013 1 32
In-place Upgrading Dirsync to Azure AD Connect
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question