Link to home
Start Free TrialLog in
Avatar of westone
westoneFlag for United States of America

asked on

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
Avatar of cybersean
cybersean

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

Avatar of westone

ASKER

'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.
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.
Avatar of westone

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Shift-3
Shift-3
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of westone

ASKER

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.