Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-01-24
13
Medium Priority
?
585 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 2000 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 2000 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
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 

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 2000 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 2000 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

After hours on line I found a solution which pointed to the inherited Active Directory permissions . You have to give/allow permissions to the "Exchange trusted subsystem" for the user in the Active Directory...
Unified and professional email signatures help maintain a consistent company brand image to the outside world. This article shows how to create an email signature in Exchange Server 2010 using a transport rule and how to overcome native limitations …
To show how to generate a certificate request 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 >> Certificates…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

722 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