How does one find out if a Windows service is installed using (preferably) only batch?

℡╲_俬逩灬. 提交于 2019-11-29 17:09:40

问题


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?


回答1:


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.




回答2:


You should using "query", not "Stop" or something else command, the "Stop" can stop your service if it's exist, then this is not the right way.

@echo OFF

set _ServiceName=SomeServiceName

sc query %_ServiceName% | find "does not exist" >nul
if %ERRORLEVEL% EQU 0 echo Service Does Not Exist.
if %ERRORLEVEL% EQU 1 echo Service Exist.



回答3:


what about:

sc interrogate "nameofyourservicehere"

I've found this really useful since tasklist won't give informations on whether the service is installed or not. (or i did not find how)




回答4:


Here is an example using sc query to verify if a windows service is installed and stop if found.

sc query | find /I "%tmpServiceName%" > nul
if not errorlevel 1 echo. && net stop %tmpServiceName%
if errorlevel 1 echo.   - Windows service %tmpServiceName% is not running or doesn't exist.



回答5:


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



来源:https://stackoverflow.com/questions/3883099/how-does-one-find-out-if-a-windows-service-is-installed-using-preferably-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!