Why: Recently we had a server issue where Server A's service required a restart, the service was not listed as Stopped but was not in an operable state to which we received the call out to ensure the service on Server B was restarted due to a vague dependency on Server A. Once back in work the next morning I decided to write a script which would help aid anyone who was on call out so that we (DBA team) didn't have to be called out in the early hours of the morning.
Function: The code basically goes away with a list of Servers/Services and runs a set of verification (i.e. is the server alive) and then attempts to restart the service regardless of the service state, any errors during any part of the processing will result in an email being generated and a break out of code ensuring that no further code is run (since you may have dependencies).
My Example: In my example we were having issues with a VMware service which had ties to NetApp storage which then had a knock on affect on our FlexClone process which we use to present reports to users on a regular refresh. To which point I would need to alter the script in the following way:
FuncRestartService "Server1" "VMwareService"
FuncRestartService "Server2" "SQLSERVICE"
You can put as many servers/services as you wish, just add another line as you go and it will treat the next line with a dependency on the previous.
My intent was with this that if the service/s was scheduled to restart then it could be done with this script therefore restarting any service that I may require on any other server, but this could be request on demand if required and still ensure that my services are up and running.
If you wanted to run this from SQL I'd suggest setting it up as a job and using the following code to reference the script, changing locations where needed:
PowerShell –Command “& { C:\flexscript\RestartServiceV1.ps1; exit $LASTEXITCODE}”
Since this does restart services, do be careful as to which servers/services you're restarting!