mardi 28 juin 2016

Nested If-Else in batch command

I am trying to check if two services are present or not. If either one is not present it should print "No" else print "Yes". What I tried is:

@echo off
set service1=present_service
set service2=not_present

sc query %service1% | find "does not exist" >nul
if %ERRORLEVEL% EQU 1 (
    sc query %service2% | find "does not exist" >nul
    if %ERRORLEVEL% EQU 1 (
      echo Yes
    ) else (
      echo No
    )    
) else (
echo No
)

If I check for the single one, it works fine. Here probably %ERRORLEVEL% is not changing its value in the second case. Even if service2 is not present, it prints Yes. Can I get any help on this.

Aucun commentaire:

Enregistrer un commentaire