Solved

Script to find Daily receive emails

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

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

Shouldn't all users have the same email signature?

You wouldn't let your users design their own business cards, would you? So, why do you let them design their own email signatures? Think of the damage they could be doing to your brand reputation! Choose the easy way to manage set up and add email signatures for all users.

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
Resolve DNS query failed errors for Exchange
To show how to create a transport rule in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Mail Flow >> Rules tab.:  To cr…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now