Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Using a powershell script to restart a service when using the services recovery function

$
0
0

Greetings,

I am trying to use a powershell script to send an e-mail message and restart a service after failure using the Service Recovery property of the service.

In short, I cannot seem to start a failed service with a script when using the recovery features for the service.

I set the service to "Run a Program" after the first failure and set the program to C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe with a command line parameter of -command c:\temp\sendalert.ps1

the script is simple 

$Server = gc env:computername
$smtpServer = "nospam.company.org"
$smtpFrom = "PrinterAlert@company.org"
$smtpTo = "admin@company.org"
$messageSubject = "TP AutoConnect service has failed on $server"
$body = "The TP AutoConnect service on $server has been restarted."

send-mailmessage -from "$smtpFrom" -to "$smtpTo" -subject "$messageSubject" -body "$body" -smtpServer "$smtpserver"

start-service -name thn32svc

When I run the script manually in powershell it works fine; the service is started.

When I test a failure of the service, the e-mail message is sent, but the service is not restarted.

I have tried 'start-service' and 'set-service' command-lets. I have also put a sleep command in so the system had 20 seconds after the failure before the start-service was issued. I also tried just starting the service with a script. None of these changes worked.

Any insight into why this wouldn't work is appreciated.

Thanks

Derek




Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>