How to check in Windows if the service is not inst

2020-04-17 04:56发布

I'm trying to do a batch program that needs to check if the service is installed before checking if it is running/stopped.

I would just like to ask if there is any way that I can check the ERRORLEVEL of an uninstalled service when the OS is Windows XP.

In my code snippet:

ver | find /I "XP"
if %errorlevel%==0 goto ver_xp
goto ver_nonXP

:ver_xp
echo Windows XP
sc query myService > nul
echo %errorlevel%
if errorlevel ___I goto ServiceOk
if errorlevel ___ goto ServiceError
goto ServiceError

:ver_nonXP
echo Windows is not XP
sc query myService > nul
echo error1_percent %errorlevel%
if %errorlevel%==0 goto ServiceOk
if %errorlevel% NEQ '0' goto ServiceError
goto end

:ServiceError
echo Service is not installed
net helpmsg %errorlevel%
goto end

:ServiceError
rem do some operations here....

I tried to use

if errorlevel 1060 goto ServiceError

It seems that if the service is not installed, the condition above will always be false.

I made the errorlevel ____ because I don't know that the correct condition should be.

2条回答
贪生不怕死
2楼-- · 2020-04-17 05:05

sc query myService |find "myService" >nul will do the trick

查看更多
地球回转人心会变
3楼-- · 2020-04-17 05:22

According to this answer this is possible in batch using method you described How does one find out if a Windows service is installed using (preferably) only batch?

Alternatively you could query with powershell:

$serv_status = get-service "myService"
if($serv_status -ne $null) 
{ 
    // do some operations here
}
查看更多
登录 后发表回答