3

I have following problem:

I want to create a batch file looping through an amount of ip addresses to stop a certain service on remote pc's.

Cause the stopping process takes some time I need to have a second loop to query the state of the service and waiting until the service reached the state STOPPED.

Set /p StartIP="Start IP (101): "
Set /p EndIP="End IP (199): "


for /L %%n IN (%StartIP%, 1, %EndeIP%) DO (

psservice -u username -p password \\192.168.8.&&n stop 'ServiceName'

:CHECK
echo 5 sek.
ping 127.0.0.1 -n 5 > nul

for /F "tokens=3 delims=: " %%H in ('psservice -u username -p password \\192.168.8.%%n query 'ServiceName' ^| findstr "        STATE"') do (
 if NOT %%H==STOPPED (
 echo.
 echo state: %%H
 goto CHECK
 )
 echo.
 echo state: %%H
)

    psservice -u username -p password \\192.168.8.%%n start 'ServiceName'
)

Now the point I stuck to:

My range of numbers '%%n' from the /L loop is getting destroyed after passing the /F loop the first time.

With an example it looks like this:

Set /p StartIP="Start IP (101): 101"
Set /p EndIP="End IP (199): 105"


for /L %%n IN (101, 1, 105) DO (

psservice -u username -p password \\192.168.8.101 stop 'ServiceName'

:CHECK
echo 5 sek.
ping 127.0.0.1 -n 5 > nul

for /F "tokens=3 delims=: " %%H in ('psservice -u username -p password \\192.168.8.101 query 'ServiceName' ^| findstr "        STATE"') do (
 if NOT %%H==STOPPED (
 echo.
 echo state: %%H
 goto CHECK
 )
 echo.
 echo state: %%H
)

    psservice -u username -p password \\192.168.8.%%n start 'ServiceName'
)

In this example I have the ip range 192.168.8.101 to 192.168.8.105. The batch is running well until the /F loop is run trough the first time. After this moment the %%n parameter from my /L loop is gone. The service can't start again, cause the %%n parameter already missing and my /L loop can't continue too -.-

Someone an idea what I can do to solve this problem?

Thank you already for reading this post.

5
  • As far as I know psservice doesn't do an async call to stop the service and exits without a response. so your second loop is entirely unnecessary? Commented Sep 23, 2016 at 6:48
  • 1
    If you use a goto command inside for loops, the loops (all nested levels) break Commented Sep 23, 2016 at 6:54
  • Thank you for your fast response :-) Without the second loop psservice is trying to start the service while the state is STOP_PENDING and not STOPPED. This cause the issue that the service will stuck permanently in state STOP_PENDING Commented Sep 23, 2016 at 6:56
  • Another approach would be to apply the n loop three times - first send the stop to each address, then check each address, then start. The advantage would be that you would have already sent the stop to each process by the time the delay is encountered for the first time, so with luck each enters the stopped state within the 5 second delay, and they're all restarted, minimising the required delay. Commented Sep 23, 2016 at 7:40
  • @Magoo this was my first attempt for the batch. In this case everything worked well but I have to stop and start about 50 services cause I have a high ip range. Problem was the downtime from the services. If I have bad connections the batch needs like 1 hour for 50 ip addresses and that's a long downtime for the services. Commented Sep 23, 2016 at 7:58

1 Answer 1

3

As MC ND said in his comment, issuing a GOTO within a FOR loop will break the loop. Actually, the issue is more general than that - issuing a GOTO within any block of code (paranthesized, or concatenated via &) will terminate the remainder of the block.

The simple solution is to extract the code that needs GOTO and put it in its own subroutine, and then CALL the routine from within your loop.

Set /p StartIP="Start IP (101):"
Set /p EndIP="End IP (199):"

for /L %%n IN (%StartIP%, 1, %EndIP%) DO (
  psservice -u username -p password \\192.168.8.%%n stop 'ServiceName'
  call :check %%n
  psservice -u username -p password \\192.168.8.%%n start 'ServiceName'
)
exit /b

:CHECK
echo 5 sek.
ping 127.0.0.1 -n 5 > nul
for /F "tokens=3 delims=: " %%H in (
  'psservice -u username -p password \\192.168.8.%1 query 'ServiceName' ^| findstr "        STATE"'
) do (
  echo.
  echo state: %%H
  if not %%H==STOPPED goto CHECK
)
exit /b
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for your answer. That sounds like an very good idea but now I have the problem that parameter %%n is not hand over to the extracted :check routine
@Exasus Then add it to the call call :check %%n and use it with psservice ...192.168.0.%1
@jeb it works like a charm :) :) :) you guys saved my day and the whole weekend :) :) :) Thank you all so much. Love this community
@Exasus - Ugh, i used your 2nd set of code without thinking and missed the need for %%n within the check. All fixed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.