Solved

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

Posted on 2008-06-20
10
337 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:radar365
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:radar365
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
What's the error on the latest code sample?
0
 

Author Closing Comment

by:radar365
Comment Utility
Wonderfull.  Many thanks for all of your assitance.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Numerous times I have been asked this questions that what is it that makes my machine log on so slow, there have been cases where computers took 23 minute exactly after taking password and getting to the desktop. Interesting thing was the fact th…
I've always wanted to allow a user to have a printer no matter where they login. The steps below will show you how to achieve just that. In this Article I'll show how to deploy printers automatically with group policy and then using security fil…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now