Sanjay Asthana
Sanjay Asthana

Reputation: 11

I want to create batch script to start the services which are down and startup type is automatic

I am able to pull the services which are down and list them in a file with the below code but need help to design the script which can check down services and if startup type is Automatic just start them.

@echo off 

wmic service where started=false get name >> stoppedservices.txt

echo "The program has completed"

Upvotes: 1

Views: 116

Answers (2)

Compo
Compo

Reputation: 38623

You could try and do it all using the same WMIC command.

WMIC Service Where "StartMode='Auto' And Started='False'" Call StartService

Just bear in mind that, some services may be either delayed auto starting or pending and this does not take such information into account.

Upvotes: 1

A Cat Named Tiger
A Cat Named Tiger

Reputation: 403

You can use sc command in the for loop to complete your script:

@echo off 
for /f "tokens=*" %%s in ('wmic service where started^=false get name') do (
    for /f "tokens=3" %%t in ('sc qc %%s ^|findstr /c:"START_TYPE"') do (
        if "%%t"=="2" net start %%s
    )
)
echo "The program has completed"
pause>nul

If desired, you can use sc start instead of net start.

net start will return success message after the startup task is completed while sc start will return message as it is processing service startup. If you are a person who likes to see the process completed step by step, net start would be better choice for you.

Upvotes: 1

Related Questions