views:

41

answers:

4

I need to check if a Windows service is installed from a batch file. I can dip into something other than batch if I need to, but I would prefer not to. Is there any way to do this?

+2  A: 

you can run "net stop [servicename]" if it fails with "the service name is invalid" the service isn't installed

Climber104
+1  A: 

I'd say to make use of PowerShell. As this is exactly the type of issue it succeeds in.

Aaron
-1 not a particularly helpful answer. Code examples would help him more than just a link to a technology.
John
@John There are code examples. Did you go to the link? To many people rely on everything being embedded in SO...doesn't make since to regurgitate information from elsewhere.
Aaron
I do prefer Powershell to batch, but I would rather not add that dependency to our build system. Thanks for your answer, though!
kerkeslager
@john - that downvote is very unfair. That page does contain plenty of examples (see Introducing If.. Else Logic to WMI Services, there's a whole script that shows how to do this in PS). Not everything needs to be duplicated on SO.
Kev
+2  A: 

Try this:

@echo off
SC QUERY ftpsvc > NUL
IF ERRORLEVEL 1060 GOTO MISSING
ECHO EXISTS
GOTO END

:MISSING
ECHO SERVICE MISSING

:END

Note that the SC QUERY command queries by the short service name not the display name. You can find this name by looking at the General tab of a service's properties in Service Manager.

Kev
A: 
James - built2order