Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
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: 2591 | Views in the last 30 days: 560
 
Related Articles
FORUM

Restart SQL Server services

Restart SQL Server services

BLOG

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

FORUM

Cluster Service Restarts Automatically

Blocking Locks in SQL Server causing Cluster Service to Restart

FORUM

SQL Server 2005 Reporting Services Failed to restart

SQL Server 2005 Reporting Services Failed to restart

FORUM

Server Restart Sequence - With Replication

Server Restart Sequence - With Replication

Tags
powershell    
service restart    
services    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones