Checking service status with .bat file

Running Windows XP, I have three services whose status I want to automatically verify.  If the three services are running I simply want to proceed to the next line in the .bat file.  I tried piping text from sc.exe query <service_name> to a variable but that didn't do anything.  Assume we have services name sc1, sc2 and sc3.

Thanks
LVL 10
wls3Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tuttlepcCommented:
0
SteveGTRCommented:
Could do something like this:
@echo off
 
setlocal enabledelayedexpansion
 
for %%a in (sc1, sc2, sc3) do call :GETSRVSTAT "%%a"&if /i not "!status!"=="Running" goto :EOF
 
echo All services are running...
 
goto :EOF
 
:GETSRVSTAT
 
set status=
 
for /f "tokens=4" %%a in ('sc query "%~1" 2^>NUL^|findstr STATE') do set status=%%a
 
goto :EOF

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wls3Author Commented:
This was a great lead.  All I had to do was modify the parameters for my situation and this worked perfectly.  Thanks.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

SteveGTRCommented:
Glad it worked :)
0
wls3Author Commented:
I found out the service name has spaces.  Aside from creating individual variables and contatinating them into a new variable, is there a way to work around this?  I got it to work doing the concat, but, it's ugly and doesn't seem efficient.
0
SteveGTRCommented:
No problem :)
@echo off
 
setlocal enabledelayedexpansion
 
for %%a in ("sc1", "sc2", "sc3") do call :GETSRVSTAT %%a&if /i not "!status!"=="Running" goto :EOF
 
echo All services are running...
 
goto :EOF
 
:GETSRVSTAT
 
set status=
 
for /f "tokens=4" %%a in ('sc query "%~1" 2^>NUL^|findstr STATE') do set status=%%a
 
goto :EOF

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.