Batch file question

Hi,
I am fairly familiar with using batch files to automate simple tasks, and use them often in conjuction with Scheduled Tasks to automate tasks that otherwise are not able to be scheduled. What I would like to do is find out if it is possible to write a batch file or script to loop through a command or series of commands, and use a variable which is replaced by the name of a computer on my network. So I could create a list of computer names on my network, then run the script and have the script reference the list of computer names, and loop through whatever commands are in the script applying each name to the variable.

For example, if I wanted to copy a particular file from the server to the System32 folder on each client, the script might contain the command: copy  filename \\remotecomputername\c$\windows\system32

How can I write it to loop and replace remotecomputername with a name from my list of computers? I realize for this example I could just use a logon script, but that is not the point. I want to learn how to use a variable list in a script.

Thanks,
Bill
westoneAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cyberseanCommented:
Interesting...  I just wrote a script to do that yesterday.  Are your computers on a domain?  If so, you can use the script below.  You will have to make some modifications related to OU and the file you want to copy.  Let me know if you have any questions with the script.  Save the text as a .vbs file and run it once you have made the changes.  

'This script copies a file from the local computer to every computer in the specified OU
 
On Error Resume Next
Const ADS_Scope_Subtree=2
 
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open = "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
 
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_Scope_Subtree
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
'Change the name and location of the log file here
objLogFile = "C:\copiedFileLog.txt"
Set objFile = objFSO.CreateTextFile(objLogFile)
 
'Change the OU here
objCommand.CommandText="Select Name from 'LDAP://ou=yourOU,dc=yourDomain,dc=com' where objectcategory='computer'"
 
Set objRecordSet=objCommand.Execute
objRecordSet.MoveFirst
 
Do Until objRecordSet.EOF
 
	strComputer = objRecordSet.Fields("Name").Value
	
	'This pings the computer before attempting to change the password.  If the computer is not reachable, it is skipped.
	Set objShell = CreateObject("WScript.Shell")
	strCommand = "%comspec% /c ping -n 3 -w 1000 " & strComputer & ""
	set objExecObject = objShell.Exec(strCommand)
 
		Do While Not objExecObject.StdOut.AtEndOfStream
			strText = objExecObject.StdOut.ReadAll()
				If Instr(strText, "Reply") > 0 Then
 
'Change the location where you want to copy the file to here
strPath = "\\" & strComputer & "\C$\Windows\system32\filename.txt"
 
'This actually copies the file, change c:\filename.txt to the file you want to copy
objFSO.CopyFile "c:\filename.txt", strPath, True
 
If Err = 0 Then	
'This is the text that is written to the log file when it succeeds
	objFile.WriteLine("Copied Filename.txt to " & strComputer & ".")					
else
'This is the text that is written to the log file when it fails
	objFile.WriteLine("Unable to copy secedit.sdb to " & strComputer & ".")
End if
					Err.Clear	
 
				else
'This is written to the log file if a computer is unreachable
					objFile.WriteLine("Unable to ping " & strComputer & ".")
				end if
		Loop
	
	objRecordSet.MoveNext
	
Loop
 
	objFile.Close
WScript.echo("The utility completed successfully.  Please check the log file " & objLogFile & " for errors.")

Open in new window

0
westoneAuthor Commented:
'This pings the computer before attempting to change the password.  If the computer is not reachable, it is skipped.

What password does this script change? I am logged in as Administrator on DC, and only targeting computers in this domain. I have access to all the drives without resetting passwords, and prefer not to change any.
0
cyberseanCommented:
I'm sorry about that.  It does NOT change any passwords.   Those comments are incorrect.  
(I copied the ping function from a previous script and it had those comments).  

It just pings the computer name.  If the computer doesn't respond, it will skip to the next section.  It should say, 'This pings the computer before attempting to copy the file.  You can change the comments, but they are safe to ignore.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

westoneAuthor Commented:
Okay, I have used a vb script to change passwords on a remote computer before, and I didn't think I saw any code that looked like it was doing that. Give me some time and I'll get back to you.
0
Shift-3Commented:
To do this in a batch script, use the FOR /F command.
http://www.ss64.com/nt/for_f.html




for /F %%G in (computerlist.txt) do echo %%G

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
westoneAuthor Commented:
Shift-3's solution answered the specific question I asked. Cybersean's solution is more sophisticated, but with Shift-3's solution I have already been able to copy Scheduled Tasks and run maintenance tasks on all the machines on this network. Plus it has the added advantage of being able to be used on workgroups and LANs with no domain just as easily, and I look after a number of those as well. Since Cybersean's script retrieves the list of computers from AD it is restricted to domain use.
Thanks for the help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.