如何确定是否仅使用(最好)批处理安装了 Windows 服务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3883099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does one find out if a Windows service is installed using (preferably) only batch?
提问by kerkeslager
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?
我需要检查是否从批处理文件安装了 Windows 服务。如果需要,我可以深入研究批处理以外的其他内容,但我不想这样做。有没有办法做到这一点?
回答by Kev
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.
请注意,该SC QUERY
命令通过短服务名称而不是显示名称进行查询。您可以通过查看服务管理器中服务属性的常规选项卡来找到此名称。
回答by M Sasan MH
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.
回答by Spangenhelm
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)
我发现这非常有用,因为tasklist
不会提供有关是否安装该服务的信息。(或者我没有找到如何)
回答by James - built2order
Here is an example using sc query
to verify if a windows service is installed and stop if found.
这是一个sc query
用于验证是否安装了 Windows 服务并在找到时停止的示例。
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.
回答by Chris Kooken
you can run "net stop [servicename]" if it fails with "the service name is invalid" the service isn't installed
您可以运行“net stop [servicename]”,如果它因“服务名称无效”而失败,则该服务未安装