Created
December 4, 2017 17:58
-
-
Save jzabroski/3e48ad2aa740f4e377243780f0f1bd70 to your computer and use it in GitHub Desktop.
Restart Windows Service and its currently running Dependent Services
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Param ( | |
[Parameter(Mandatory=$true)] [String]$ServiceName | |
) | |
<# | |
From: https://gallery.technet.microsoft.com/PowerShell-Script-for-8243e5d1 | |
This is a PowerShell Script to restart a windows service with its dependent services. The dependent services that were not running when the restart request made, will not be started, so that the system services state will remain same after restart service script run. | |
#> | |
[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 restart $($ServiceInput.Name)" | |
if ($ServicesToRestart.Contains($ServiceInput.Name) -eq $false) | |
{ | |
Write-Host "Adding service to restart $($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($ServiceToStart in $ServicesToRestart) | |
{ | |
Write-Host "Start Service $ServiceToStart" | |
Start-Service $ServiceToStart -Verbose | |
} | |
Write-Host "-------------------------------------------" | |
Write-Host "Restart of services completed" | |
Write-Host "-------------------------------------------" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment