Solved

Script to find Daily receive emails

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

This article explains how to install and use the NTBackup utility that comes with Windows Server.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
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)
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

634 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