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 "-------------------------------------------" |
To create a reusable Release Management tool setup as shown below.
A component using the tool can be created.
The component available for release template.
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.
No comments:
Post a Comment