Windows command to get service status?

Olivier Tremblay picture Olivier Tremblay · Apr 29, 2009 · Viewed 151.1k times · Source

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.

Answer

Rômulo Ceccon picture Rômulo Ceccon · Apr 29, 2009

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.