Solved

Script to find Daily receive emails

Posted on 2013-12-16
2
506 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
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

This article explains in simple steps how to renew expiring Exchange Server Internal Transport Certificate.
Read this checklist to learn more about the 15 things you should never include in an email signature.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

679 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