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.
gotocommand insideforloops, the loops (all nested levels) breaknloop three times - first send thestopto each address, then check each address, thenstart. The advantage would be that you would have already sent thestopto each process by the time the delay is encountered for the first time, so with luck each enters thestoppedstate within the 5 second delay, and they're all restarted, minimising the required delay.