Solved

Batch file help adding to servers to txt file

Posted on 2013-06-13
9
353 Views
Last Modified: 2013-06-14
I have a script that will run a couple commands, but I want it to read all of the servers in the text file to run the commands below...


 copy /y test.cmd "\\%\c$\test.cmd"
 copy /y test.pst "\\%\c$\outlook\pst"
 start psexec \\% c:\test.cmd
0
Comment
Question by:mystikal1000
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 4

Assisted Solution

by:Christopher Raymond Mendoza
Christopher Raymond Mendoza earned 50 total points
ID: 39246683
I hope this helps:

http://ss64.com/nt/for_f.html
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39246919
To elaborate on that something like this should do you.  The bit in the for loop between the ( and ) is ran once for each line in the text file, taking the first"token" off each line - if you have more than one item on each line other than just the server name we can adjust to pickup the right bit.

Depending upon what you have there already you might want to do some checks of whether files are copied, whether directory exists and create it if not etc.

Steve

@echo off
set servers=c:\temp\servers.txt
set log=c:\temp\logfile.txt

for /f "tokens=1" %%s in ('type "%servers%') do (
 echo Working on %%s
 copy /y test.cmd "\\%%s\c$\test.cmd"
 copy /y test.pst "\\%%s\c$\outlook\pst"
 start psexec \\%%s c:\test.cmd
)
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39246922
FOR /F is the correct hint, but there is more to do:
@echo off
for /F "tokens=*" %%C in (c:\Temp\Servers.txt) do (
  copy /y test.cmd "\\%%C\c$\test.cmd"
  copy /y test.pst "\\%%C\c$\outlook\pst"
  psexec \\%%C -d c:\test.cmd
)

Open in new window

I have used the "detach" option of PsExec instead of spawning it with start, because it will not open new windows on your desktop that way.
0
 
LVL 1

Author Comment

by:mystikal1000
ID: 39247436
Dragon-IT - when I run the file I am just getting the output and its not going further...
set servers=c:\temp\servers.txt
set log=c:\temp\logfile.txt

Qlemo - when I try to run this i don't get anything from echo on, it just vanishes.

I would like to get this fixed as soon as you can help me.  I appreciate it!
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 43

Expert Comment

by:Steve Knight
ID: 39247459
Ok so you have a file "C:\temp\servers.txt" or whatever which has all your computer names in, one per line, viewable in notepad, say, OK?

If you try this way does it show you the names?

@echo off
set servers=c:\temp\servers.txt

for /f "tokens=1" %%s in ('type "%servers%') do (
 echo Working on %%s
)
pause 

Open in new window


What did you call the batch file, not copy.cmd, for.cmd or anything else similar that would cause it to run itself again?
0
 
LVL 1

Author Comment

by:mystikal1000
ID: 39247482
i called it a different name.  When I ran your batch file I get this error...

C:\Temp>test.cmd
The syntax of the command is incorrect.
C:\Temp>
0
 
LVL 1

Author Comment

by:mystikal1000
ID: 39247493
Yes I am just testing one server in C:\temp\servers.txt folder, looks like its just not reading the file.
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 425 total points
ID: 39247506
Sorry I see I have missed a " in the copy/paste.  It should be like this.... or use Qlemo suggestion.  I used 'type "filename"' in cae you use a server filename with spaces in, as then you need quotes around it, i.e. "C:\my dir\filename.txt" and that causes the for command to work differently.

Steve


@echo off
set servers=c:\temp\servers.txt

for /f "tokens=1" %%s in ('type "%servers%"') do (
 echo Working on %%s
 copy /y test.cmd "\\%%s\c$\test.cmd"
 copy /y test.pst "\\%%s\c$\outlook\pst"
 start psexec \\%%s c:\test.cmd
) 

Open in new window

0
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 25 total points
ID: 39247613
BTW: For tests, never run a batch file by clicking it - you won't see errors. Start a cmd prompt, and execute the batch from there.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
A safe way to clean winsxs folder from your windows server 2008 R2 editions
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…

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

22 Experts available now in Live!

Get 1:1 Help Now