Solved

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

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

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
Want to promote your upcoming event?

Are you going to an event? Are you going to be exhibiting at a tradeshow? Talking at a conference? Using a promotional banner in your email signature ensures that your organization’s most important contacts stay in the know and can potentially spread the word about the event.

 
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

Why won’t your email signature format correctly?

Struggling to get your corporate email signatures to format correctly? Does the logo keep resizing? Is the text appearing too big? What can you do to prevent this? Find out how you can save your signatures today.

Question has a verified solution.

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

Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
Follow this checklist to learn more about the 15 things you should never include in an email signature from personal quotes, animated gifs and out-of-date marketing content.
In this video we show how to create a Contact 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 Recipients >> Contact ta…
This video discusses moving either the default database or any database to a new volume.

896 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

11 Experts available now in Live!

Get 1:1 Help Now