Reputation: 11
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
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
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