Solved

Script to find Daily receive emails

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
Find out what you should include to make the best professional email signature for your organization.
The viewer will learn how to count occurrences of each item in an array.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

734 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