Restart services which is in hung or stopped mode using PowerShell

0
(0)

At times you may need to open and manage your Windows Services. You may want to stop some service, start it, disable the service, delay its start-up or resume or pause the Windows Service. At such time, the Service Manager, which is a build-in tool in the Windows operating system, will help you. But sometime services go to hung mode and we can’t restart it. This post will show you how to restart the service which is in hung mode using command prompt as well as PowerShell script to restart set of services in given list of machines.

Windows Services are applications that typically start when the computer is booted and run quietly in the background until it is shut down. Strictly speaking, a service is any Windows application that is implemented with the services API and handles low-level tasks that require little or no user interaction.

How to open Windows Service Manager

Below is the steps to open Windows Services Manager on your machine:

  1. Right-click on the Start button to open the WinX Menu
  2. Select Run
  3. Type services.msc in the Run box and press Enter.
  1. Windows Services Manager will open.

In Service Manager you will be able to start, stop, disable, delay Windows services.

Here, you will see the list of Services running on your system, along with their description. You will also be able to see their Status -whether they are running or stopped, along with the Start-up types.

Types of Windows Services start-up:

Windows offers four start-ups types:

  1. Automatic
  2. Automatic (Delayed Start)
  3. Manual
  4. Disabled.

Problem Statement:

Sometimes services go to hung/Becoming unresponsive. This may be due to deadlock, crashing of the process. This can be resolved by killing the hung process running in the system forcefully.

After the service appears to hang, you can go to a command-line and type sc queryex servicename. This should give you the current STATE of the service.

Here is how to fix the Issue

  1. Open the command prompt as Administrator
  2. Run sc queryex <Service Name>
  1. Get PID for offending service or instance name
  2. run taskkill /PID <PID> /f

Another way is to Monitor the services

Below script will retrieve services you specify for monitoring, on a remote system you specify, and check if the services are stoppped or hung and try to restart.

#Machine to be monitored 
$Computer = "Hostname" 
 
#Create an array of all services running 
$GetService = get-service -ComputerName $Computer 
 
#Mention the services you want to monitor 
$ServiceArray = "Service1"," Service2"," Service3"," Service4";
 
#Find service that is stopped 
foreach ($Service in $GetService) 
{ 
    foreach ($srv in $ServiceArray) 
    { 
        if ($Service.name -eq $srv) 
        { 
            #check if a service is hung 
            if ($Service.status -eq "StopPending") 
            {  
            $servicePID = (gwmi win32_Service | where { $_.Name -eq $srv}).ProcessID 
            Stop-Process $ServicePID 
            Start-Service -InputObject (get-Service -ComputerName $Computer -Name $srv) 
            } 
            # check if a service is stopped 
            elseif ($Service.status -eq "Stopped") 
            {  
            #automatically restart the service. 
            Start-Service -InputObject (get-Service -ComputerName $Computer -Name $srv) 
            } 
        } 
    } 
}

I hope you find the post useful. If any question you can add them in comment section. If you find the post useful, please rate, comment and share the post.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

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