Solved

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

Posted on 2013-01-24
13
562 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
Don't lose your head updating email signatures!

Do your end users still have the wrong email signature? Do email signature updates bore you or fill you with a sense of dread? You can make this a whole lot easier on yourself by trusting an Exclaimer email signature management solution. Over 50 million users do...so should you!

 
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

Do email signature updates give you a headache?

Do you spend too much time managing email signatures? Hate visiting every user’s desk to make updates? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Well, let Exclaimer give your company the email signature it deserves!

Join & Write a Comment

Resolve Outlook connectivity issues after moving mailbox to new Exchange 2016 server
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
In this video we show how to create a mailbox database 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 Servers >> Data…
how to add IIS SMTP to handle application/Scanner relays into office 365.

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

19 Experts available now in Live!

Get 1:1 Help Now