Solved

How to modify an existing batch file to add FOR loop to run on 50+ servers?

Posted on 2008-06-20
10
340 Views
Last Modified: 2011-10-19
Hello,

I have the follwong batch files I am loggin into each server and running them locally on each one.  I have a list of 50+ servers that this need to be run on and I believe I need to add a FOR loop on this or maybee this needs to be run via psexec?  Please Advise?  Much thanks in advance.

Batch File #1
\\sa1esm99\RCMD$\install.bat

cd C:\Program Files\cchecker2\

weekly.bat /nowait


Batch File #2
del C:\1sct\conf\sgs.conf

cd C:\1sct\bin\

sgsc.exe
0
Comment
Question by:radar365
  • 5
  • 4
10 Comments
 
LVL 6

Expert Comment

by:JapyDooge
ID: 21831583
So sa1esm99 is the servername that has to be changed each run?
Is there a logic in these names? for example sa1esm01-sa1esm99?
0
 
LVL 13

Expert Comment

by:TheCapedPlodder
ID: 21831592
Hi,

If you are running each batch file on fifty servers you will need to use a tool like psexec.

You will want a batch file like this:

for %%a in (server1 server2 server3 etc.) do call :start

:start
if exist \\%%a\c$\batchfile1.bat goto batchfile2
copy batchfile1.bat \\%%a\c$

:batchfile2
if exist \\%%a\c$\batchfile1.bat goto execute
copy batchfile2.bat \\%%a\c$

:execute
psexec.exe \\%%a batchfile1.bat
psexec.exe \\%%a batchfile2.bat

obviously you will need to provide the correct syntax for psexec.

Cheers,

TCP
0
 
LVL 13

Expert Comment

by:TheCapedPlodder
ID: 21831653
I should add, you can also use a file that contains the list of server names e.g.:

for %%a in ('inputlist.txt') do call :start
etc....

The inputlist.txt file would look like this:

server1
server2
server3
etc.

Let me know if you need anything clarifying

Cheers,

TCP
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:radar365
ID: 21832576
Thanksyou have definitly got me on the right track.  I have modified your script with my specifics it now looks like this:

for %%a in (C:\pstools\new folder\srvlst_test.txt) do call :start

:start
if exist \\%%a\c$\Run_1st_CCTool.bat goto Run_2nd_SGS.bat
copy Run_1st_CCTool.bat \\%%a\c$

:Run_2nd_SGS.bat
if exist \\%%a\c$\Run_2nd_SGS.bat goto execute
copy Run_2nd_SGS.bat \\%%a\c$

:execute
psexec.exe \\%%a -c Run_1st_CCTool.bat
psexec.exe \\%%a -c Run_2nd_SGS.bat

However I get the following error when running this:
Microsoft Windows [Version 5.2.3790]
(C) Copyright 1985-2003 Microsoft Corp.
C:\Pstools\New Folder>cc-run.bat

C:\Pstools\New Folder>for %a in (C:\pstools\new folder\srvlst_test.txt) do call
:start

C:\Pstools\New Folder>call :start

C:\Pstools\New Folder>if exist \\%a\c$\Run_1st_CCTool.bat goto Run_2nd_SGS.bat

C:\Pstools\New Folder>copy Run_1st_CCTool.bat \\%a\c$
The network path was not found.
        0 file(s) copied.

C:\Pstools\New Folder>if exist \\%a\c$\Run_2nd_SGS.bat goto execute

C:\Pstools\New Folder>copy Run_2nd_SGS.bat \\%a\c$
The network path was not found.
        0 file(s) copied.

C:\Pstools\New Folder>psexec.exe \\%a -c Run_1st_CCTool.bat

PsExec v1.55 - Execute processes remotely
Copyright (C) 2001-2004 Mark Russinovich
Sysinternals - www.sysinternals.com

Couldn't access %a:
The network path was not found.

Make sure that the default admin$ share is enabled on %a.

C:\Pstools\New Folder>psexec.exe \\%a -c Run_2nd_SGS.bat

PsExec v1.55 - Execute processes remotely
Copyright (C) 2001-2004 Mark Russinovich
Sysinternals - www.sysinternals.com

Couldn't access %a:

However I was able to get the follwing to work: psexec \\servername -c C:\Run_1st_cctool.bat

I tried modified this with your suggestion and ended up with:

for %%a in (srvlst_test.txt) do call :execute

:execute
psexec.exe \\%%a -c Run_1st_CCTool.bat
psexec.exe \\%%a -c Run_2nd_SGS.bat

When I run this I get the following results:
Microsoft Windows [Version 5.2.3790]
(C) Copyright 1985-2003 Microsoft Corp.

C:\Pstools\New Folder>cctest.bat

C:\Pstools\New Folder>for %a in (srvlst_test.txt) do call :execute

C:\Pstools\New Folder>call :execute

C:\Pstools\New Folder>psexec.exe \\%a -c Run_1st_CCTool.bat

PsExec v1.55 - Execute processes remotely
Copyright (C) 2001-2004 Mark Russinovich
Sysinternals - www.sysinternals.com

Couldn't access %a:
The network path was not found.

Make sure that the default admin$ share is enabled on %a.

I know I am extremly close, thanks for your continued assitance.
 
0
 
LVL 13

Expert Comment

by:TheCapedPlodder
ID: 21835128
You need to put the name of the input file inside single quotes:

for %%a in ('C:\pstools\new folder\srvlst_test.txt') do call :start

Cheers,

TCP
0
 

Author Comment

by:radar365
ID: 21845706
Even with quotes still the same error:

Couldn't access %a:
The network path was not found.
Make sure that the default admin$ share is enabled on %a.

Here is the batch file as of now:

for %%a in ("C:\pstools\new_folder\SRVLST.txt") do call :start

goto end

:start
psexec.exe \\%%a -c Run_1st_CCTool.bat
psexec.exe \\%%a -c Run_2nd_SGS.bat

:end

Echo Host script ran today %date%>%0\..\log.txt

There is something unny going on with this.  For some reason it cant reference the server list??? Any other ideas?  Thanks.
CCTest.txt
0
 
LVL 13

Accepted Solution

by:
TheCapedPlodder earned 500 total points
ID: 21845774
OK, I can see why this might be an issue.

Try the attached code snippet instead.
for /f %%a in (srvlst_test.txt) do set server=%%a & call :start
goto end
 
:start
if exist \\%server%\c$\Run_1st_CCTool.bat goto Run_2nd_SGS.bat
copy Run_1st_CCTool.bat \\%server%\c$
 
:Run_2nd_SGS.bat
if exist \\%server%\c$\Run_2nd_SGS.bat goto execute
copy Run_2nd_SGS.bat \\%server%\c$
 
:execute
psexec.exe \\%server% -c Run_1st_CCTool.bat
psexec.exe \\%server% -c Run_2nd_SGS.bat
 
:end

Open in new window

0
 

Author Comment

by:radar365
ID: 21845810
The strange this is when I run "psexec.exe \\servername -c Run_1st_CCTool.bat" it works fine.  This indicates to me that there is no problem accessing the admin$ share on %a???  Thanks for your continued assistance with this issue.  You are soo close to getting all the points on this one.
0
 
LVL 13

Expert Comment

by:TheCapedPlodder
ID: 21845837
What's the error on the latest code sample?
0
 

Author Closing Comment

by:radar365
ID: 31469152
Wonderfull.  Many thanks for all of your assitance.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many of us need to configure DHCP server(s) in their environment. We can do that simply via DHCP console on server or using MMC snap-in on each computer with Administrative Tools installed in a network. But what if we have to configure many DHCP ser…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question