Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2008-06-20
10
Medium Priority
?
345 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

610 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