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
:arce 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
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
:arce 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