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
Example 2:-
PS P:\>Restart-Service -SERVERNAME HQVD0026 -SERVICENAME MSSQLSERVER
Example 3:-Negate the “$” meaning by using “`” for Named Instance.
PS P:\>Restart-Service -SERVERNAME HQVD0026 -SERVICENAME SQLAgent`$KAT
Download the code here Restart-Service
Thanks for reading my space…
Happy Learning!!