Function Stop-ServiceAndDependencies


I have a problem with the Stop-Serviceanddependencies function.

  #Stop the parent service
				Write-Log -Message "Stop parent service [$($Service.ServiceName)] with display name [$($Service.DisplayName)]." -Source ${CmdletName}
				[ServiceProcess.ServiceController]$Service = Stop-Service -InputObject (Get-Service -ComputerName $ComputerName -Name $Service.ServiceName -ErrorAction 'Stop') -Force -PassThru -WarningAction 'SilentlyContinue' -ErrorAction 'Stop'

sometimes A service stay in StopPending and the script blocked in the function.

One of solution would be to add the code part to the function

$Services = Get-WmiObject -Class win32_service -Filter "state = 'stop pending'"
if ($Services) {
foreach ($service in $Services) {
try {
Stop-Process -Id $service.processid -Force -passThru -EA Continue
catch {
Write-Host " Error. Error details: $_.Exception.Message"
else {
Write-Host "No services with 'Stopping'.status"

but I can’t add it in the function

Thanks for helping me

Well StopPending means that something already attempted to stop the service. Dont think doing it again will help.

What error does it throw? Or does it just do nothing but block the process?