Have you ever felt the need to restart any of the windows service and its dependent services? There might be situation where you have to periodically restart the windows service due to various reasons.
One reason I have come across to restart windows service at specific period was when one of our domain controller's lsass process kept on utilizing memory and gradually increased the memory that ultimately resulted into a hung server.
We needed to find the root cause of the issue but until then we also needed to keep the services provided by domain controller up and running. As a temporary solution, we created the script to restart active directory services and it's dependent services which we configured to run at specific interval using task scheduler.
The script along with restarting the main service and it's dependent services, also checks whether any of the service has went into hang state. If any of the service is found to be in hang state, it will fetch the PID of the hang service and will kill the corresponding process. It will then attempt to restart the service once again.
The same script can be used to restart any other windows service as well. You just need to provide the name of the main service. Script will automatically get the dependent services.
The name of the main service should be mentioned in place of "ntds". Below lines of code will get the service name and it's dependent services.
#Get the ADDS and its dependent services
$set1 = @((Get-Service -Name ntds).Name)
$set2 = Get-Service -Name ntds -dependent | foreach {$_.name}
$set3 = $set1 + $set2
Below mentioned code will initiate the command to restart the services. It will store the result in apowershell job. Later you can check the status of the job. The script will then wait for 120 seconds to restart the services.
#Restart the ADDS and its dependent services
$job = start-job -scriptblock {Restart-Service -name ntds -force}
#Give time to restart the services
start-sleep -Seconds 120
Below mentioned code will check for the state of each service and if any of the service went in hung mode will restarting, it will kill the corresponding process and will reinitiate the restart of the service.
#Check status of service and if required restart the service
foreach ($service in $set3)
{$status=(get-service -name $service).status
If ($status -eq "Stopping")
{$PI=Get-WmiObject -Class Win32_Service -Filter "Name LIKE '$service'" | Select-Object -ExpandProperty ProcessId
taskkill.exe /PID $PI /F
Start-Sleep -Seconds 5
}
Restart-Service -Name $service
}
Complete script can be found below:
#Get the ADDS and its dependent services
$set1 = @((Get-Service -Name ntds).Name)
$set2 = Get-Service -Name ntds -dependent | foreach {$_.name}
$set3 = $set1 + $set2
#Restart the ADDS and its dependent services
$job = start-job -scriptblock {Restart-Service -name ntds -force}
#Give time to restart the services
start-sleep -Seconds 120
#Check status of service and if required restart the service
foreach ($service in $set3)
{$status=(get-service -name $service).status
If ($status -eq "Stopping")
{$PI=Get-WmiObject -Class Win32_Service -Filter "Name LIKE '$service'" | Select-Object -ExpandProperty ProcessId
taskkill.exe /PID $PI /F
Start-Sleep -Seconds 5
}
Restart-Service -Name $service
}