Pick up windows services fulfilling 2 conditions

1

I need to keep running windows services, except when doing maintenance work, since it is the only time where you define startup type = manual and the service is stopped.

So far I have this code, but I can not detect that the "startup type" is different from "manual" to just run the net start.

@echo off
net start | find "wuauserv" > nul 2>&1 
if not .%errorlevel%.==.0. net start "wuauserv"
    
asked by mario miranda 10.05.2017 в 20:50
source

1 answer

2

You can use sc to ask for configuration information ( qc , query configuration ) of a service:

  

qc: Queries the configuration information for a service.

     

qc: Check the configuration information of a service.

Example of use:

C:\Windows\system32>sc qc AppMgmt
[SC] QueryServiceConfig SUCCESS

SERVICE_NAME: AppMgmt
        TYPE               : 20  WIN32_SHARE_PROCESS
        START_TYPE         : 3   DEMAND_START
        ERROR_CONTROL      : 1   NORMAL
        BINARY_PATH_NAME   : C:\Windows\system32\svchost.exe -k netsvcs
        LOAD_ORDER_GROUP   :
        TAG                : 0
        DISPLAY_NAME       : Application Management
        DEPENDENCIES       :
        SERVICE_START_NAME : LocalSystem

As you can see, it has a manual start ( DEMAND_START ).

Example in which I check the boot mode and the status of the service:

@echo off
sc qc %1 | find "AUTO_START" > NUL
if not errorlevel 0 goto fin

sc query %1 | find "STOPPED" > NUL
if not errorlevel 0 goto fin

echo Arrancando el servicio %1
sc start %1

:fin
echo Trabajo finalizado

First I check if it is automatic start ( AUTO_START ), in any other case I finish the execution.

If it is an automatic start service I check if it is in the stopped state ( STOPPED ), in any other case I finish the execution (eye, it could be in the state PAUSED , PAUSE_PENDING , CONTINUE_PENDING , START_PENDING or STOP_PENDING .

In the end, if the service is configured for automatic start and is stopped, the boot is ordered.

    
answered by 10.05.2017 в 21:23