Batch file question

Posted on 2008-11-06
Last Modified: 2012-05-05
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.

Question by:westone
    LVL 4

    Expert Comment

    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
    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 & ".")					
    '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
    'This is written to the log file if a computer is unreachable
    					objFile.WriteLine("Unable to ping " & strComputer & ".")
    				end if
    WScript.echo("The utility completed successfully.  Please check the log file " & objLogFile & " for errors.")

    Open in new window


    Author Comment

    '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.
    LVL 4

    Expert Comment

    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.

    Author Comment

    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.
    LVL 38

    Accepted Solution

    To do this in a batch script, use the FOR /F command.

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

    Open in new window


    Author Comment

    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.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

    734 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

    23 Experts available now in Live!

    Get 1:1 Help Now