Solved

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

Posted on 2013-01-24
13
573 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Following basic email etiquette rules will help you write a professional email and achieve a good, lasting impression with your contacts.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video we show how to create a Resource Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: Navigate to the Recipients >> Resources tab.: "Recipients" is our default selection …
In this Micro Video tutorial you will learn the basics about Database Availability Groups and How to configure one using a live Exchange Server Environment. The video tutorial explains the basics of the Exchange server Database Availability grou…

738 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