Solved

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

Posted on 2008-06-20
10
343 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
Setting up a Microsoft WSUS update system is free relatively speaking if you have hard disk space and processor capacity.   However, WSUS can be a blessing and a curse. For example, there is nothing worse than approving updates and they just have…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

717 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