Windows command to get service status?

155,298

Solution 1

Using Windows Script:

Set ComputerObj = GetObject("WinNT://MYCOMPUTER")    
ComputerObj.Filter = Array("Service")    
For Each Service in ComputerObj    
    WScript.Echo "Service display name = " & Service.DisplayName    
    WScript.Echo "Service account name = " & Service.ServiceAccountName    
    WScript.Echo "Service executable   = " & Service.Path    
    WScript.Echo "Current status       = " & Service.Status    
Next

You can easily filter the above for the specific service you want.

Solution 2

Have you tried sc.exe?

C:\> for /f "tokens=2*" %a in ('sc query audiosrv ^| findstr STATE') do echo %b
4  RUNNING

C:\> for /f "tokens=2*" %a in ('sc query sharedaccess ^| findstr STATE') do echo %b
1  STOPPED

Note that inside a batch file you'd double each percent sign.

Solution 3

You can call net start "service name" on your service. If it's not started, it'll start it and return errorlevel=0, if it's already started it'll return errorlevel=2.

Solution 4

Using pstools - in particular psservice and "query" - for example:

psservice query "serviceName"

Solution 5

look also hier:

NET START | FIND "Service name" > nul IF errorlevel 1 ECHO The service is not running

just copied from: http://ss64.com/nt/sc.html

Share:
155,298

Related videos on Youtube

Olivier Tremblay
Author by

Olivier Tremblay

I'm just some kind of weird zombie programmer from the late fourties, born in the mid-eighties. Don't ask. I've got a wife and 2 kids, and I'll be using this forum to provide myself and others with knowledge, mine or otherwise, obtained through brain eating or otherwise.

Updated on July 09, 2022

Comments

  • Olivier Tremblay
    Olivier Tremblay almost 2 years

    I need to know the status of a service at the end of my batch script which restarts services using "net stop thingie" and "net start thingie".

    In my most favorite ideal world, I would like to e-mail the state to myself, to read on cold winter nights, to reassure myself with the warmth and comfort of a server that I know is running right.

    Just for you to know, I'm using a Windows server 2003 platform, and a batch file seemed the best choice. I don't mind using something else, and would be very open to suggestions, but just for the sake of knowledge (as a zombie craves brains, I thought, why not inflate my own), is there a command that allows me to check on the status of a service, in command line?

    Should I just redirect the output of the command to a file?

    Where the hell are my pants? (Gosh, I really do hope the humor inserted in this will not insult anyone. It's Wednesday morning, and humor I do need too :P)

    [Edit:] The solution I used is (no longer) available for download from --link redacted--

    It is used as a task set to be executed during the night, and checking my e-mail in the morning, I see whether or not the service has correctly restarted.

  • Olivier Tremblay
    Olivier Tremblay about 15 years
    Selected because no installation required. Thank you guys!
  • Olivier Tremblay
    Olivier Tremblay about 15 years
    That combined with the other script that I have selected as a good answer, I got my stop/restart/mail result cycle right about done.
  • JRL
    JRL about 15 years
    Not sure why I got a downvote on this but since he's using a bat file to start the service with the net start command, and it's built into the command to return an error code, the easiest is to simply check the error code. But if it's mandatory to create a separate vbs for it instead of checking the return, I don't care.
  • Nam G VU
    Nam G VU about 10 years
    This is my favourite ^^
  • Fr0sT
    Fr0sT over 8 years
    + for "NET START", not obvious way to get a list of running services
  • Fr0sT
    Fr0sT over 8 years
    All this stuff with temp file could be replaced with (NET START | FIND "NameOfService" > nul) && SET ServiceRunning=1 or GOTO instead of setting the flag.
  • Jesper Mygind
    Jesper Mygind over 4 years
    Or simpler (but similar): for /f "tokens=4" %a in ('sc query YOURSERVICENAME ^| findstr STATE') do echo %a