Link to home
Start Free TrialLog in
Avatar of radar365
radar365Flag for United States of America

asked on

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

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
Avatar of JapyDooge
JapyDooge
Flag of Netherlands image

So sa1esm99 is the servername that has to be changed each run?
Is there a logic in these names? for example sa1esm01-sa1esm99?
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
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
Avatar of radar365

ASKER

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.
 
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
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
ASKER CERTIFIED SOLUTION
Avatar of TheCapedPlodder
TheCapedPlodder
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
What's the error on the latest code sample?
Wonderfull.  Many thanks for all of your assitance.