Now its time to use a vbs script to ensure that all services are stopped and started in the correct way – so if any services are dependent on other they will be restated as well.

First Create a new task from your Console:

image

Let it be an Agent Tasks and Remember the Management Pack:

image

Give it a Name and a Target like this:

image

image

Add a Parameter – either your Service shortname or a text like this:

Press [OK] and [Create].

image

Choose a server and [Restart a Services and its dependencies]

image

When running te task you need to create a override for the service which should be restarted:

image

As a test we use the Workstation service – this service have other services which is depenent on it.

image

And Run IT

image

And the result – all services which is dependent on Workstation services and the Workstation Services itselft have been restarted.

image

 

And the Script to paste in your Task:

' //***************************************************************************
' // ***** Script Header *****
' //
' // File:      RestartServiceAndAntecedents.vbs
' // Author:    Coretech A/S. https://blog.ctglobalservices.com
' // Purpose:   Restarts a service and all its antecendents
' //
' // Usage: cscript.exe RestartServiceAndAntecedents.vbs "ServiceShortName"
' //
' // CORETECH A/S History:
' // 1.0.0    KRA 12/04/2011  Created initial version.
' // 1.0.1    JGS 12/04/2011  Added while loops and functions
' //
' // ***** End Header *****
' //***************************************************************************
'//----------------------------------------------------------------------------
'//
'//  Global constant and variable declarations
'//
'//----------------------------------------------------------------------------
strComputer = "."
strService = Wscript.Arguments(0)

Dim oServiceDic

'//----------------------------------------------------------------------------
'//  Main routines
'//----------------------------------------------------------------------------

'List of services to restart
Set oServiceDic = CreateObject("scripting.dictionary")

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colServiceList = objWMIService.ExecQuery("Associators of " _
    & "{Win32_Service.Name='" & strService & "'} Where " _
        & "AssocClass=Win32_DependentService " & "Role=Antecedent" )

For Each objService in colServiceList
    If objService.State = "Running" Then
        oServiceDic.Add objService.Name, objService.Name
        StopService objService.Name
    End If
Next

'Restart the Service (this causes all antecedent services to stop too, no need to do it manually)
RestartService strService

For Each key In oServiceDic.Keys
    StartService oServiceDic(key)
Next

'//----------------------------------------------------------------------------
'//  Procedures
'//----------------------------------------------------------------------------

Function RestartService(strService)
    StopService strService
    StartService strService
End Function

Function StopService(strService)
    Set colServiceList = objWMIService.ExecQuery _
            ("Select * from Win32_Service where Name='" & strService & "'")
    For Each objService in colServiceList
        objService.StopService()
    Next
    Do
        bLoop = False
        Set colServiceList = objWMIService.ExecQuery _
            ("Select * from Win32_Service where Name='" & strService & "'")
        For Each objService in colServiceList
            If Not objService.State = "Stopped" Then
             bLoop = True
            End If
        Next
        WScript.Sleep 1000    
    Loop While bLoop     
End Function

Function StartService(strService)
    Set colServiceList = objWMIService.ExecQuery _
            ("Select * from Win32_Service where Name='" & strService & "'")
    'Restart the service
    For Each objService in colServiceList
         objService.StartService()
    Next
    Do
        bLoop = False
        Set colServiceList = objWMIService.ExecQuery _
            ("Select * from Win32_Service where Name='" & strService & "'")
        For Each objService in colServiceList
            If Not objService.State = "Running" Then
             bLoop = True
            End If
        Next
        WScript.Sleep 1000    
    Loop While bLoop 
End Function