Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

PowerSQL By Prashanth Jayaram

Technology enthusiast with 7+ years of experience in Database Technology. I am Microsoft Certified Professional with certificates of OCA, MCP, MCTS, MCITP developer, MCITP administration and backed with a degree in Master of Computer Application. My expertise lies in T-SQL programming, Replication, PowerShell and Performance Tuning. Hobbies are Drawing, playing soccer and listening to Melodies songs.

PowerShell – Restart Service – Local or Remote Machines

This post is to start the services of local or remote computers. It also check for all its dependent services.  The script works on below logic

  • Service status is Running

The script will stop the dependent services ,stop the main service , start the dependent service and then start the main service.

  • Service status is stopped 

Start the dependent service and start the main service

I’ve tested this script by executing it on SQL Services(both default and Named instance). Please do a thorough testing before executing it across production. The different ways of executing it is shown below. You can customize this script as per your requirement.

The step by step details are given below

Code Details -

********************************************************************************

Function Restart-Service
{
PARAM([STRING]$SERVERNAME=$ENV:COMPUTERNAME,[STRING]$SERVICENAME)
#Default instance – MSSQLSERVER  ,
#Named instance is MSSQL$KAT – Try to retain its value by negating “$” meaning using “`
#hence you need to pass service name like MSSQL`$KAT
$SERVICE = GET-SERVICE -COMPUTERNAME $SERVERNAME -NAME $SERVICENAME -ERRORACTION SILENTLYCONTINUE
IF( $SERVICE.STATUS -EQ “RUNNING” )
{
$DEPSERVICES = GET-SERVICE -COMPUTERNAME $SERVERNAME -Name $SERVICE.SERVICENAME -DEPENDENTSERVICES | WHERE-OBJECT {$_.STATUS -EQ “RUNNING”}
IF( $DEPSERVICES -NE $NULL )
{
FOREACH($DEPSERVICE IN $DEPSERVICES)
{
Stop-Service -InputObject (Get-Service -ComputerName $SERVERNAME -Name $DEPSERVICES.ServiceName)
}
}
Stop-Service -InputObject (Get-Service -ComputerName $SERVERNAME -Name $SERVICE.SERVICENAME) -Force
if($?)
{
Start-Service -InputObject (Get-Service -ComputerName $SERVERNAME -Name $SERVICE.SERVICENAME)
$DEPSERVICES = GET-SERVICE -COMPUTERNAME $SERVERNAME -NAME $SERVICE.SERVICENAME -DEPENDENTSERVICES | WHERE-OBJECT {$_.STATUS -EQ “STOPPED”}
IF( $DEPSERVICES -NE $NULL )
{
FOREACH($DEPSERVICE IN $DEPSERVICES)
{
Start-Service -InputObject (Get-Service -ComputerName $SERVERNAME -Name $DEPSERVICE.SERVICENAME)
}
}
}
}
ELSEIF ( $SERVICE.STATUS -EQ “STOPPED” )
{
Start-Service -InputObject (Get-Service -ComputerName $SERVERNAME -Name $SERVICE.SERVICENAME)
$DEPSERVICES = GET-SERVICE -COMPUTERNAME $SERVERNAME -NAME $SERVICE.SERVICENAME -DEPENDENTSERVICES | WHERE-OBJECT {$_.STATUS -EQ “STOPPED”}
IF( $DEPSERVICES -NE $NULL )
{
FOREACH($DEPSERVICE IN $DEPSERVICES)
{
Start-Service -InputObject (Get-Service -ComputerName $SERVERNAME -Name $DEPSERVICE.SERVICENAME)
}
}
}
ELSE
{
“THE SPECIFIED SERVICE DOES NOT EXIST”
}
}

*******************************************************************************

Example 1:- Local Machine( Restart SQL Service locally)

PS P:\> Restart-Service -SERVICENAME MSSQLSERVER

Restart-Service3

Example 2:-

PS P:\>Restart-Service -SERVERNAME HQVD0026 -SERVICENAME MSSQLSERVER

Image

Example 3:-Negate the “$” meaning by using “`” for Named Instance.

PS P:\>Restart-Service -SERVERNAME HQVD0026 -SERVICENAME SQLAgent`$KAT

Image

Download the code here Restart-Service

Thanks for reading my space…

Happy Learning!!


Comments

Leave a comment on the original post [sqlpowershell.wordpress.com, opens in a new window]

Loading comments...