locked
Find out if a service is running RRS feed

  • Question

  • Hey all,

    I am trying to write a script that will call a service, find out if it is running,  if it is, all is good. If not, it will stop a second service, then start both services in a specific order.

    Workflow -- 

    1. Find out if service A is "running" or "stopped".

    2. If service A is stopped, stop service B.

    3. Once service B is stopped, start service A, then start service B.

    4. If service A is running,  don't do anything.

    This is what I have so far:

    function FuncCheckService{
     param($ServiceName)
     $arrService = Get-Service -Name gupdate
     if ($arrService.Status -ne "Running"){
     Stop-Service clicktorunsvc
     Start-Service gupdate
     Start-Service clicktorunsvc
    
     }
     if ($arrService.Status -eq "running"){ 
     Write-Host "$ServiceName service is already started"
     }
     }

    Those are the services I am playing with, not the actual services that will be used. 

    Thanks in advance.


    • Edited by scottylat2486 Wednesday, November 1, 2017 8:57 PM Clarity
    Tuesday, October 31, 2017 3:10 PM

All replies

  • And what is the problem?


    \_(ツ)_/

    Tuesday, October 31, 2017 3:17 PM
  • When I just tested it on my workstation, gupdate service did not start when I ran the script. So I am not sure where to go.
    Tuesday, October 31, 2017 3:23 PM
  • Can you start and stop it from the GUI?  If not then you need to fix the service.  Note that some services do not stay running.  They start once and stop then start on demand due to a system event.

    What is GPUpdate?  It is a process and not a service.


    \_(ツ)_/


    • Edited by jrv Tuesday, October 31, 2017 3:29 PM
    Tuesday, October 31, 2017 3:28 PM
  • You were correct on that one, it was the service I was choosing.  I have since updated the script to services that stop and start correctly from the GUI.  One thing I did find is I need powershell to be open as Admin, otherwise I get an error when running "start-service service".  So, I ran the script in an admin powershell and it still was not successful. 

    Two questions:

    1. How do I run a ps1 script as admin?

    2. Is there any change I need to the script to have it run correctly?

    Thanks.

    Tuesday, October 31, 2017 5:01 PM
  • You have to run an elevated session to "Run As Admin.  This is a basic function of Windows.  Any EXE that you right click on will have a menu item "Run As Administrator".  That is how to open an elevated session.

    Open an elevated session and call the script.

    You need to take some time to learn basic Windows operations for an administrator,  You cannot directly launch a PS1 as an Admin.  Elevation only works for exe  files.


    \_(ツ)_/

    Tuesday, October 31, 2017 5:11 PM
  • This is a ps1 script, not a exe.  So no, it does not have run as admin.   And I am calling the script, not powershell. If that is the case, I will figure it out with our management software, since the management software calls the script, not the program. 

    Think I figured out the issues with the script though, I had a bit more there then needed.  It seems to be running correctly now, just need to figure out how to call it. 

    Tuesday, October 31, 2017 5:26 PM
  • Hi,

    Based on my research, you could have a try with the following demo scripts to achieve your workflow. Hope it is helpful to you:
    $serviceA = 'BITS'
    $serviceB = 'wuauserv'
    
    if ((Get-Service -Name $serviceA).Status -ne 'Running')
    {
        Write-Host "$serviceA is not running, stopping $serviceB"
        Stop-Service $serviceB
        do
        {
            $serviceB_status = (Get-Service -Name $serviceB).Status
            if ($serviceB_status -ne 'Stopped')
            {
                Start-Sleep -Seconds 1
                Write-Host "Stopping $serviceB"
            }
        }
        until ($serviceB_status -eq 'Stopped')
        Write-Host "$serviceB is stopped, starting $serviceA"
    
        Start-Service $serviceA
        do
        {
            $serviceA_status = (Get-Service -Name $serviceA).Status
            if ($serviceA_status -ne 'Running')
            {
                Start-Sleep -Seconds 1
                Write-Host "Starting $serviceA"
            }
        }
        until ($serviceA_status -eq 'Running')
        Write-Host "$serviceA is started, starting $serviceB"
    
        Start-Service $serviceB
        do
        {
            $serviceB_status = (Get-Service -Name $serviceB).Status
            if ($serviceB_status -ne 'Running')
            {
                Start-Sleep -Seconds 1
                Write-Host "Starting $serviceB"
            }
        }
        until ($serviceB_status -eq 'Running')
        Write-Host "$serviceB is started, operation success"
    }
    else
    {
        Write-Host "$serviceA is already running."
    }

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, November 1, 2017 5:06 AM
  • Hi,

    Just checking in to see if the information provided was helpful. Does the script work?

    Please let us know if you would like further assistance.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Friday, November 3, 2017 7:15 AM
  • Hi,

    I am checking how the issue is going, if you still have any questions, please feel free to contact us.

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.

    Appreciate for your feedback.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Tuesday, November 7, 2017 8:20 AM