Solved

Make a scheduled task to run an EMS powershell command and send me the output via email

Posted on 2013-01-24
13
579 Views
Last Modified: 2013-01-30
I have been tasked with checking DAG replication between 2 exchange 2010 servers.  I just want to run Test-ReplicationHealth and some other EMS commands and get the output sent to me via email.  This is so I can avoid having to manually run the commands on many different environments.

I have seen how you can create the task and run the EMS cmd at the desired time but not sure how to go about getting the output sent via email.
0
Comment
Question by:gambit120
[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
  • 7
  • 6
13 Comments
 
LVL 5

Assisted Solution

by:coraxal
coraxal earned 500 total points
ID: 38816562
Well....you can use the Send-MailMessage cmdlet..something like this:

 
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin -ErrorAction SilentlyContinue

Test-ReplicationHealth -Identity "emailservername"

Send-MailMessage -SmtpServer "emailservername" -Body "script ran Ok" -From "MailAdmin <mailadmin@donotreply.com>" -To "itadmin@yourdomain.com" -Subject "Exchange Health Run"

Open in new window

0
 

Author Comment

by:gambit120
ID: 38816595
Will line 1 be needed if these commands will be run from an exchange server?
0
 
LVL 5

Assisted Solution

by:coraxal
coraxal earned 500 total points
ID: 38816652
No, in that case the snapin is already loaded...you can remove it if you wish, but I find it useful if the script gets moved.

I noticed that you want to get the output of the cmdlet emailed to you....you can redirect the output of the cmdlet to a text file and then attach it
$outputfile = "C:\temp\exchangehealthfile.txt"

Test-ReplicationHealth -Identity "emailservername" >> $outputfile

Send-MailMessage -From "MailAdmin <mailadmin@donotreply.com>" -To "itadmin@yourdomain.com" -Subject "Exchange Health Run" -Body "script ran Ok" -Attachments $outputfile -SmtpServer "emailservername" 

Open in new window

0
Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 

Author Comment

by:gambit120
ID: 38817019
How do I make that a scheduled task? Sorry I'm a noob at this.
0
 
LVL 5

Expert Comment

by:coraxal
ID: 38817042
Have a look at this question...should get you going in the right direction http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_27999894.html

"Scheduled Task Exchange 2010 powershell fails to execute properly"
0
 

Author Comment

by:gambit120
ID: 38837960
Ok I have got this going fine now. But one more thing,, when I run the script the results in the output file duplicate within it. So say if the task is run twice it includes 2 x results inside the txt file.  What does the script need to include to overwite the old outputted file so I only get the single result in the txt file?
0
 
LVL 5

Assisted Solution

by:coraxal
coraxal earned 500 total points
ID: 38837987
Change this line
Test-ReplicationHealth -Identity "emailservername" >> $outputfile

Open in new window

To:
Test-ReplicationHealth -Identity "emailservername" > $outputfile

Open in new window

0
 
LVL 5

Accepted Solution

by:
coraxal earned 500 total points
ID: 38837998
Or you can check for the file explicitly and delete it before writing to it
if(Test-Path $outputfile -PathType Leaf)
{
	Write-Host "File exists.."
	Remove-Item $outputfile -Force
} else {
	Write-Host "File does not exist"
}
	
Test-ReplicationHealth -Identity "emailservername" > $outputfile

Open in new window

Either way should prevent the duplicate output
0
 

Author Comment

by:gambit120
ID: 38838017
Thanks coraxal. Is it possible to output the txt file contents into the body of the email?
0
 
LVL 5

Expert Comment

by:coraxal
ID: 38838045
Yes, you can store the contents of the file in a variable and pass it to the -Body parameter
$outputfile = "C:\temp\exchangehealthfile.txt"

Test-ReplicationHealth -Identity "emailservername" > $outputfile

$body = $outputfile | Out-String

Send-MailMessage -From "MailAdmin <mailadmin@donotreply.com>" -To "itadmin@yourdomain.com" -Subject "Exchange Health Run" -Body $body -Attachments $outputfile -SmtpServer "emailservername"

Open in new window

0
 

Author Comment

by:gambit120
ID: 38838138
Hmm I only get the C:\temp\exchangehealthfile.txt written in the body of the email, am I doing something wrong?
0
 
LVL 5

Assisted Solution

by:coraxal
coraxal earned 500 total points
ID: 38838140
Sorry, missed the cmdlet  Get-Content
$body = Get-Content $outputfile | Out-String

Open in new window

0
 

Author Comment

by:gambit120
ID: 38838165
Cheers that worked although the columns were not aligned but I will look into that myself
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

615 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