Friday, 7 August 2015

Restart Services with Dependant Services – Using PowerShell to Create RM Component

In release management there is a tool available to restart a service. This works fine as long as there is no other service depending on the service being restarted. If there is any dependant service this action throws an error.
1
image
Investigation in WWW revealed using a PowerShell to do this is not that easy. Few important links on this is here.
https://support.software.dell.com/appassure/kb/128574
https://social.technet.microsoft.com/Forums/scriptcenter/en-US/574d175f-c370-4bd5-9bf1-c5e400362dfa/powershell-script-to-restart-a-service-and-its-dependents-and-their-dependents?forum=ITCG&prof=required
http://www.blogmynog.com/2010/06/22/powershell-script-to-restart-service-and-dependents/
Each solution in above link had some sort of an issue. Some did not work if there is dependency in another dependant service. Some solution not have the correct order of stop start sequence.  Some does not start only the services that were running earlier.
Out of above
https://social.technet.microsoft.com/Forums/scriptcenter/en-US/574d175f-c370-4bd5-9bf1-c5e400362dfa/powershell-script-to-restart-a-service-and-its-dependents-and-their-dependents?forum=ITCG&prof=required
the question (by Scott W. Sander) itself had a great code segment. A little bit of enhancement to that script made below powershell script which is perfectly working. Download from here https://gallery.technet.microsoft.com/PowerShell-Script-for-8243e5d1


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
Param (
[Parameter(Mandatory=$true)] [String]$ServiceName
)

[System.Collections.ArrayList]$ServicesToRestart = @()

function Custom-GetDependServices ($ServiceInput)
{
 #Write-Host "Name of `$ServiceInput: $($ServiceInput.Name)"
 #Write-Host "Number of dependents: $($ServiceInput.DependentServices.Count)"
 If ($ServiceInput.DependentServices.Count -gt 0)
 {
  ForEach ($DepService in $ServiceInput.DependentServices)
  {
   #Write-Host "Dependent of $($ServiceInput.Name): $($Service.Name)"
   If ($DepService.Status -eq "Running")
   {
    #Write-Host "$($DepService.Name) is running."
    $CurrentService = Get-Service -Name $DepService.Name
    
                # get dependancies of running service
    Custom-GetDependServices $CurrentService                
   }
   Else
   {
    Write-Host "$($DepService.Name) is stopped. No Need to stop or start or check dependancies."
   }
   
  }
 }
    Write-Host "Service to Stop $($ServiceInput.Name)"
    if ($ServicesToRestart.Contains($ServiceInput.Name) -eq $false)
    {
        Write-Host "Adding service to stop $($ServiceInput.Name)"
        $ServicesToRestart.Add($ServiceInput.Name)
    }
}

# Get the main service
$Service = Get-Service -Name $ServiceName

# Get dependancies and stop order
Custom-GetDependServices -ServiceInput $Service


Write-Host "-------------------------------------------"
Write-Host "Stopping Services"
Write-Host "-------------------------------------------"
foreach($ServiceToStop in $ServicesToRestart)
{
    Write-Host "Stop Service $ServiceToStop"
    Stop-Service $ServiceToStop -Verbose #-Force
}
Write-Host "-------------------------------------------"
Write-Host "Starting Services"
Write-Host "-------------------------------------------"
# Reverse stop order to get start order
$ServicesToRestart.Reverse()

foreach($ServiceToRestart in $ServicesToRestart)
{
    Write-Host "Start Service $ServiceToRestart"
    Start-Service $ServiceToRestart -Verbose
}
Write-Host "-------------------------------------------"
Write-Host "Restart of services completed"
Write-Host "-------------------------------------------"

image



To create a reusable Release Management tool setup as shown below.

2


A component using the tool can be created.

3 

The component available for release template.

4

When used with Release template it manages to stop dependant services first and stop the requested service. Then restart the requested service and the dependant services.

5

image

image

No comments:

Popular Posts