Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Batch: errorlevel for sc stop <service> is inconsistent

Avatar of shootbox
shootboxFlag for Italy asked on
ProgrammingWindows Batch
13 Comments1 Solution2439 ViewsLast Modified:
Hi,

Part of my script's purpose is to stop a service remotely and then wait for this service to stop before proceeding.
It's written in the following way:

            @Echo Stopping Watchdog service...
            sc \\%%a stop Watchdog
            :waitsomemore
            timeout 5 >nul
            sc \\%%a query Watchdog | find /i "STOPPED"
            if %errorlevel%!==!0 goto :waitsomemore
            @Echo Watchdog service stopped

Whereas %%a is an IP address from a list of IPs (that part works great).

The problem I am seeing, is that depending on where I run this from / to, the errorlevel for this command:
            sc \\%%a query Watchdog | find /i "STOPPED"
Will sometimes still be 1 after the service stopped. which means it will just loop and never continue.

So narrowing down my big pain to this problem only, should the errorlevel be 1 for this line, until the "find" actually finds the "STOPPED" string and then it should return 0?

Many thanks in advance