problems with enbeded for loop

  • Thread starter Thread starter Kevin Kramer
  • Start date Start date
K

Kevin Kramer

I need some help. I am working on a project to copy the backup files from
the DHCP servers on the network. I would like the list of servers to be
dynamic. I am using netsh to give me the list and then I am parcing the
list. This works well The problem that I am having is an enbedded for
statement. See code. I tried to have the :forloop enbeded in the do loop
and also as you see it now.

The script fails at this command '%bin%\netsvc DHCPSERVER \\%server%
/query'. for some reason the netscv is failling to connect to the remote
server. I can copy the command in the command window and it will connect.
Why will it not work within the script? How can I get this to work?



@echo off
set root=%cd%
set target=\\corpdc01\dhcp$
set bin=%root%\tools


::get a list of DHCP servers that have been authrized in the domain.
del %root%\temp1.txt
netsh dhcp show server >> %root%\temp1.txt

pause
::parce out the computer names
if exist %root%temp2.txt del %root%\temp2.txt
FOR /F "skip=1 tokens=2,3* delims=[]." %%a in (%root%\temp1.txt) do (
echo %%a >> %root%\temp2.txt
)

::check to see if the dhcpserver service is running on our list


::###### Problem loop
:forloop
FOR /F "Tokens=1-4" %%x in ('%bin%\netsvc DHCPSERVER \\%server% /query') do
(
echo %%x
echo %%y
echo %%z
if %%z=="running" set ERRORLEVEL=1
if %%z=="stopped" set ERRORLEVEL=2
if %%z=="paused" set ERRORLEVEL=3
echo errorlevel is %errorlevel%
if %ERRORLEVEL% EQU 3 echo error connecting to %%b
if %ERRORLEVEL% EQU 1 echo DHCPSERVER IS RUNNING ON %%b
if %ERRORLEVEL% EQU 2 echo service is paused on %%b
)

::loop through the list and get the error levels
:skip

::####################
:: Problem area
FOR /F %%b in (%root%\temp2.txt) do (
set errorlevel=0
set server=%%b
goto forloop
)

:: #######end problem

goto cleanup

:: this is were I stopped because I can not get the for loop to work
###############################################################################

::IF %errorlevel% EUQ 1 echo %server% >> %root%\running.txt


::create the backup scripts and execute them on servers running DHCP
servers.

FOR /F %%b in (%root%\running.txt do (
echo echo. > %root%\%%b.bat
echo set target=%target%
echo copy %systemroot%\system32\dhcp\backup\dhcpcfg
\\%target%\%computername%-dhcpcfg >> %root%\%%b.bat
type %root%\%%b.bat
pause
%bin%\psexec \\%%b -c %root%\%%b.bat
del %%b.bat
)

:cleanup

del /y *.txt
del svcstat.bat
 
1. Since you never set the %SERVER% variable prior to using it, you were
failing on the first invocation of netsvc with:
%bin%\netsvc DHCPSERVER \\ /query
2. Your loop through temp2.txt does not work to increment SERVER to point to
all valid servers retrieved via "netsh dhcp show server"

Actually, you can write the whole thing without needing temp1.txt and
temp2.txt (I show the improvement as "Option B"). If you can pipe output to
generate temp1.txt, you can use FOR to parse it as-is, without the
intermediate file to generate the list of servers -- which you don't need
temp2.txt as a list because you can directly use that in the second loop.


Option A: Try the following instead of your entire ::###### Problem loop
segment

set errorlevel=0
FOR /F %%b in (%root%\temp2.txt) do (
FOR /F "Tokens=1-4" %%x in ('%bin%\netsvc DHCPSERVER \\%%b /query') do
(
echo %%x
echo %%y
echo %%z
if %%z=="running" set ERRORLEVEL=1
if %%z=="stopped" set ERRORLEVEL=2
if %%z=="paused" set ERRORLEVEL=3
echo errorlevel is %errorlevel%
if %ERRORLEVEL% EQU 3 echo error connecting to %%b
if %ERRORLEVEL% EQU 1 echo DHCPSERVER IS RUNNING ON %%b
if %ERRORLEVEL% EQU 2 echo service is paused on %%b
)
)


Option B: Try this instead of your existing script

@echo off
set root=%cd%
set target=\\corpdc01\dhcp$
set bin=%root%\tools

set ERRORLEVEL=0
FOR /F "skip=1 tokens=2,3* delims=[]." %%a in ( 'netsh dhcp show server' )
DO (
REM %%a holds all computer names (i.e. it is temp2.txt)

FOR /F "tokens=1-4" %%x in ( '%bin%\netsvc DHCPSERVER \\%%a /query' ) DO
(
IF /I "%%z" EQU "running" (
ECHO DHCP SERVER IS RUNNING ON %%a

ECHO echo. > %root%\%%a.bat
ECHO set target=%target%
ECHO copy %systemroot%\system32\dhcp\backup\dhcpcfg
%target%\%computername%-dhcpcfg >> %root%\%%a.bat
type %root%\%%a.bat
PAUSE
%bin%\psexec \\%%a -c %root%\%%a.bat
del %%a.bat

) ELSE IF /I "%%z" EQU "paused" (
ECHO DHCP service is paused on %%a
) ELSE IF /I "%%z" EQU "stopped" (
ECHO Error connecting to %%a
)
)
)


--
//David
IIS
http://blogs.msdn.com/David.Wang
This posting is provided "AS IS" with no warranties, and confers no rights.
//
I need some help. I am working on a project to copy the backup files from
the DHCP servers on the network. I would like the list of servers to be
dynamic. I am using netsh to give me the list and then I am parcing the
list. This works well The problem that I am having is an enbedded for
statement. See code. I tried to have the :forloop enbeded in the do loop
and also as you see it now.

The script fails at this command '%bin%\netsvc DHCPSERVER \\%server%
/query'. for some reason the netscv is failling to connect to the remote
server. I can copy the command in the command window and it will connect.
Why will it not work within the script? How can I get this to work?



@echo off
set root=%cd%
set target=\\corpdc01\dhcp$
set bin=%root%\tools


::get a list of DHCP servers that have been authrized in the domain.
del %root%\temp1.txt
netsh dhcp show server >> %root%\temp1.txt

pause
::parce out the computer names
if exist %root%temp2.txt del %root%\temp2.txt
FOR /F "skip=1 tokens=2,3* delims=[]." %%a in (%root%\temp1.txt) do (
echo %%a >> %root%\temp2.txt
)

::check to see if the dhcpserver service is running on our list


::###### Problem loop
:forloop
FOR /F "Tokens=1-4" %%x in ('%bin%\netsvc DHCPSERVER \\%server% /query') do
(
echo %%x
echo %%y
echo %%z
if %%z=="running" set ERRORLEVEL=1
if %%z=="stopped" set ERRORLEVEL=2
if %%z=="paused" set ERRORLEVEL=3
echo errorlevel is %errorlevel%
if %ERRORLEVEL% EQU 3 echo error connecting to %%b
if %ERRORLEVEL% EQU 1 echo DHCPSERVER IS RUNNING ON %%b
if %ERRORLEVEL% EQU 2 echo service is paused on %%b
)

::loop through the list and get the error levels
:skip

::####################
:: Problem area
FOR /F %%b in (%root%\temp2.txt) do (
set errorlevel=0
set server=%%b
goto forloop
)

:: #######end problem

goto cleanup

:: this is were I stopped because I can not get the for loop to work
############################################################################
###

::IF %errorlevel% EUQ 1 echo %server% >> %root%\running.txt


::create the backup scripts and execute them on servers running DHCP
servers.

FOR /F %%b in (%root%\running.txt do (
echo echo. > %root%\%%b.bat
echo set target=%target%
echo copy %systemroot%\system32\dhcp\backup\dhcpcfg
\\%target%\%computername%-dhcpcfg >> %root%\%%b.bat
type %root%\%%b.bat
pause
%bin%\psexec \\%%b -c %root%\%%b.bat
del %%b.bat
)

:cleanup

del /y *.txt
del svcstat.bat
 
Thanks, Option B works! I was beating my head into the wall.

I had it working until I looked at the output of netsvc DHCPSERVER command.
I saw DHCP servers that have been decommissioned still being listed in the
directory. Then I found out that netsvc does not have exit codes.


David Wang said:
1. Since you never set the %SERVER% variable prior to using it, you were
failing on the first invocation of netsvc with:
%bin%\netsvc DHCPSERVER \\ /query
2. Your loop through temp2.txt does not work to increment SERVER to point
to
all valid servers retrieved via "netsh dhcp show server"

Actually, you can write the whole thing without needing temp1.txt and
temp2.txt (I show the improvement as "Option B"). If you can pipe output
to
generate temp1.txt, you can use FOR to parse it as-is, without the
intermediate file to generate the list of servers -- which you don't need
temp2.txt as a list because you can directly use that in the second loop.


Option A: Try the following instead of your entire ::###### Problem loop
segment

set errorlevel=0
FOR /F %%b in (%root%\temp2.txt) do (
FOR /F "Tokens=1-4" %%x in ('%bin%\netsvc DHCPSERVER \\%%b /query') do
(
echo %%x
echo %%y
echo %%z
if %%z=="running" set ERRORLEVEL=1
if %%z=="stopped" set ERRORLEVEL=2
if %%z=="paused" set ERRORLEVEL=3
echo errorlevel is %errorlevel%
if %ERRORLEVEL% EQU 3 echo error connecting to %%b
if %ERRORLEVEL% EQU 1 echo DHCPSERVER IS RUNNING ON %%b
if %ERRORLEVEL% EQU 2 echo service is paused on %%b
)
)


Option B: Try this instead of your existing script

@echo off
set root=%cd%
set target=\\corpdc01\dhcp$
set bin=%root%\tools

set ERRORLEVEL=0
FOR /F "skip=1 tokens=2,3* delims=[]." %%a in ( 'netsh dhcp show server' )
DO (
REM %%a holds all computer names (i.e. it is temp2.txt)

FOR /F "tokens=1-4" %%x in ( '%bin%\netsvc DHCPSERVER \\%%a /query' )
DO
(
IF /I "%%z" EQU "running" (
ECHO DHCP SERVER IS RUNNING ON %%a

ECHO echo. > %root%\%%a.bat
ECHO set target=%target%
ECHO copy %systemroot%\system32\dhcp\backup\dhcpcfg
%target%\%computername%-dhcpcfg >> %root%\%%a.bat
type %root%\%%a.bat
PAUSE
%bin%\psexec \\%%a -c %root%\%%a.bat
del %%a.bat

) ELSE IF /I "%%z" EQU "paused" (
ECHO DHCP service is paused on %%a
) ELSE IF /I "%%z" EQU "stopped" (
ECHO Error connecting to %%a
)
)
)


--
//David
IIS
http://blogs.msdn.com/David.Wang
This posting is provided "AS IS" with no warranties, and confers no
rights.
//
I need some help. I am working on a project to copy the backup files from
the DHCP servers on the network. I would like the list of servers to be
dynamic. I am using netsh to give me the list and then I am parcing the
list. This works well The problem that I am having is an enbedded for
statement. See code. I tried to have the :forloop enbeded in the do loop
and also as you see it now.

The script fails at this command '%bin%\netsvc DHCPSERVER \\%server%
/query'. for some reason the netscv is failling to connect to the remote
server. I can copy the command in the command window and it will connect.
Why will it not work within the script? How can I get this to work?



@echo off
set root=%cd%
set target=\\corpdc01\dhcp$
set bin=%root%\tools


::get a list of DHCP servers that have been authrized in the domain.
del %root%\temp1.txt
netsh dhcp show server >> %root%\temp1.txt

pause
::parce out the computer names
if exist %root%temp2.txt del %root%\temp2.txt
FOR /F "skip=1 tokens=2,3* delims=[]." %%a in (%root%\temp1.txt) do (
echo %%a >> %root%\temp2.txt
)

::check to see if the dhcpserver service is running on our list


::###### Problem loop
:forloop
FOR /F "Tokens=1-4" %%x in ('%bin%\netsvc DHCPSERVER \\%server% /query')
do
(
echo %%x
echo %%y
echo %%z
if %%z=="running" set ERRORLEVEL=1
if %%z=="stopped" set ERRORLEVEL=2
if %%z=="paused" set ERRORLEVEL=3
echo errorlevel is %errorlevel%
if %ERRORLEVEL% EQU 3 echo error connecting to %%b
if %ERRORLEVEL% EQU 1 echo DHCPSERVER IS RUNNING ON %%b
if %ERRORLEVEL% EQU 2 echo service is paused on %%b
)

::loop through the list and get the error levels
:skip

::####################
:: Problem area
FOR /F %%b in (%root%\temp2.txt) do (
set errorlevel=0
set server=%%b
goto forloop
)

:: #######end problem

goto cleanup

:: this is were I stopped because I can not get the for loop to work
############################################################################
###

::IF %errorlevel% EUQ 1 echo %server% >> %root%\running.txt


::create the backup scripts and execute them on servers running DHCP
servers.

FOR /F %%b in (%root%\running.txt do (
echo echo. > %root%\%%b.bat
echo set target=%target%
echo copy %systemroot%\system32\dhcp\backup\dhcpcfg
\\%target%\%computername%-dhcpcfg >> %root%\%%b.bat
type %root%\%%b.bat
pause
%bin%\psexec \\%%b -c %root%\%%b.bat
del %%b.bat
)

:cleanup

del /y *.txt
del svcstat.bat
 
Back
Top