Re: problems with enbeded for loop

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: David Wang [Msft] (someone_at_online.microsoft.com)
Date: 02/05/05


Date: Fri, 4 Feb 2005 20:35:57 -0800

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.
//
"Kevin Kramer" <kevin@nospamwrev.org> wrote in message
news:Oj4cEYzCFHA.3324@TK2MSFTNGP15.phx.gbl...
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


Relevant Pages

  • Re: Scheduled tasks copied between servers often do not run
    ... I will check the Event viewer, it must be worth a visit for sure. ... I can try to reproduce the behaviour with just a stub cmd file like you've ... We are implementing dynamic Job monitoring across all the servers using ... echo %date% %time% Start of task> c:\test.log ...
    (microsoft.public.windowsxp.general)
  • Re: Scheduled tasks copied between servers often do not run
    ... the Task Scheduler relies totally on %SystemRoot%\tasks. ... AFAIK no registry entries are involved. ... We are implementing dynamic Job monitoring across all the servers using ... echo %date% %time% Start of task> c:\test.log ...
    (microsoft.public.windowsxp.general)
  • Re: Scheduled tasks copied between servers often do not run
    ... different images, to 4 different servers ... The scheduled tasks run-times are distributed over 24 hours ... the Task Scheduler's log file. ... echo %date% %time% Start of task> c:\test.log ...
    (microsoft.public.windowsxp.general)
  • Re: Scheduled tasks copied between servers often do not run
    ... After copying tasks between servers and applying passwords, ... The scheduled tasks run-times are distributed over 24 hours ... the Task Scheduler's log file. ... echo %date% %time% Start of task> c:\test.log ...
    (microsoft.public.windowsxp.general)