SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

PowerShell - Restart Services

By Rik Thwaites,

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!

Total article views: 2956 | Views in the last 30 days: 2
Related Articles

Restart SQL Server services

Restart SQL Server services


Restart the SQL Server Agent Service using PowerShell

Just a quick one today chaps, to show how you can restart the SQL Server Agent Service using PowerSh...


Cluster Service Restarts Automatically

Blocking Locks in SQL Server causing Cluster Service to Restart


SQL Server 2005 Reporting Services Failed to restart

SQL Server 2005 Reporting Services Failed to restart


SQL Agent job to show SQL Server service details upon agent restart

To show SQL Server and physical server details upon instance restart so that we have visibility of u...

service restart