Posted On November 24, 2021

How To Stop, Start, Restart a Windows Service Being Stuck in ‘Stopping’ Status

kimconnect 0 comments
blog.KimConnect.com >> Codes >> How To Stop, Start, Restart a Windows Service Being Stuck in ‘Stopping’ Status

Convenient Function:
(related to https://blog.kimconnect.com/powershell-kill-a-windows-service-forcefully)

# forceRestartService.ps1
# version 0.0.2
$serviceName='Spooler'

function forceRestartService($serviceName='Spooler'){    
    $processId=Get-WmiObject -Class Win32_Service -Filter "Name LIKE '$serviceName'"|Select-Object -ExpandProperty ProcessId
    if($processId.count -eq 1){
        try{
            $taskkill=taskkill /f /pid $processid
            write-host $taskkill
            start-service $serviceName
            $result=get-service $serviceName
            if($result.Status -eq 'Running'){
                write-host "$serviceName has successfully restarted" -foregroundcolor GREEN
                return $true
            }else{
                write-host "$serviceName has NOT successfully restarted" -foregroundcolor RED
                return $false
            }
        }catch{
            write-warning $_
            return $false
        }
    }else{
        write-warning "$serviceName is not found on $env:computername"
        return $null
    }
}

forceRestartService $serviceName
# restartWindowsService.ps1
# version 0.0.1
$serviceName='spooler'

function restartWindowsService($serviceName){
    $waitSeconds=40
    $isValidProcess=try{[bool](get-service $serviceName -EA Stop)}catch{$false}
    if($isValidProcess){
        try{
            $process=Start-Process -FilePath powershell.exe -ArgumentList "-Command Restart-Service $serviceName" -PassThru -NoNewWindow
            $process|Wait-Process -Timeout $waitSeconds -ErrorAction Stop
            write-host "$serviceName has been started successfully" -ForegroundColor Green
            return $true
        }catch{
            write-warning $_
            # $process|Stop-Process -Force
            # $processId=(get-process $serviceName).Id
            $processId=Get-WmiObject -Class Win32_Service -Filter "Name LIKE '$serviceName'"|Select-Object -ExpandProperty ProcessId
            if($processId){
                $processName=Get-Process -Id $processId
                write-host "Program now forcefully kills PID $processId of process $processName belong to service $serviceName"
                $null=$processId|%{taskkill /f /pid $_} # works more reliably than Stop-Process $processName -Force
                Start-Service $serviceName
                $started=$(try{if((get-service $serviceName).Status -eq 'Running'){$true}else{$false}}catch{$false})
                if($started){
                    write-host "'$serviceName' status is now in 'Running' status" -ForegroundColor Green
                    return $true
                }else{
                    write-warning "'$serviceName' status is in '$((get-service $serviceName).Status)' status" -foregroundcolor Red
                    return $false
                }
            }else{
                write-warning "Service '$serviceName' PID not found."
                return $false        
            }
            return $false
        }
    }else{
        write-warning "$serviceName doesn't match a valid Process Name on $env:computername"
        return $null
    }
}
  
restartWindowsService $serviceName
Symptom:
# Example error of Hyper-V Virtual Machine Management Service
Service 'Hyper-V Virtual Machine Management (vmms)' cannot be stopped due to the following error: Cannot stop vmms
service on computer '.'.
+ CategoryInfo : CloseError: (System.ServiceProcess.ServiceController:ServiceController) [Restart-Service], ServiceCommandException
+ FullyQualifiedErrorId : CouldNotStopService,Microsoft.PowerShell.Commands.RestartServiceCommand
+ PSComputerName : HYPERV05
Resolution:
# Kill the process id prior to attempting to start the service
$serviceName='vmms'
$processId=(get-process $serviceName).Id
$null=taskkill /f /pid $processId
Start-Service $serviceName
PS C:\Windows\system32> taskkill /f /pid $processId
SUCCESS: The process with PID 21404 has been terminated.
PS C:\Windows\system32> restart-service vmms
WARNING: Waiting for service 'Hyper-V Virtual Machine Management (vmms)' to stop...
WARNING: Waiting for service 'Hyper-V Virtual Machine Management (vmms)' to stop...
WARNING: Waiting for service 'Hyper-V Virtual Machine Management (vmms)' to stop...
WARNING: Waiting for service 'Hyper-V Virtual Machine Management (vmms)' to stop...
WARNING: Waiting for service 'Hyper-V Virtual Machine Management (vmms)' to stop...

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: Downloading File Error ‘Internet Explorer engine is not available’

Error Message: PS C:\temp> wget http://download.windowsupdate.com/d/msdownload/update/software/secu/2022/01/windows10.0-kb5009546-x64_d3ab97e9f811d7bf19c268e5e6b5e00e92e110ed.msu wget : The response content cannot be parsed because…

Basic CSS: Add Different Margins to Each Side of an Element

<style>.injected-text {margin-bottom: -25px;text-align: center;}.box {border-style: solid;border-color: black;border-width: 5px;text-align: center;}.yellow-box {background-color: yellow;padding: 10px;}.red-box {background-color: crimson;color: #fff;margin-top:…

PowerShell: Hyper-V Management

Migrate Live Virtual Machines (In Clustered Environment): # Connect to Hyper-V Host$remoteHost="HYPERV01"Enter-PsSession $remoteHost # Move…