Target a list of names
If you have a file with one name per line - be they user names, or more commonly computer names, this script will read them all and execute a block of your code for each name in the file. The comments provide an example that you can completely replace. The example connects to WMI and gets the computer’s service pack version; you can completely replace the entire contents of the DoObject subroutine. Within that subroutine, the strName variable has the current name from the list.
- '
- ' ScriptingAnswers.com Essentials - by Don Jones
- '
- ' Targeting a list of computer
- ' This example shows how to read in a list of
- ' names (computer names, in this case), one at
- ' a time, and then do something with each name.
- ' You can easily add your own code to this
- ' template to make it more useful! Recommend
- ' running this from CScript as it produces a
- ' lot of output.
- Dim strFilename
- strFilename = "C:\computers.txt"
- Sub DoObject(strName)
- 'this is where YOUR code goes: strName will contain
- 'the current computer (or whatever) name. In this example,
- 'we'll use it to query the service pack number. Notice
- 'how the error trapping works.
- 'first, turn on error trapping
- On Error Resume Next
- 'try to make a WMI connection - note the use of the
- 'computer name from our strName variable
- Dim objWMI, colOS, objOS
- Set objWMI = GetObject("winmgmts:\\" & strName & "\root\cimv2")
- 'did an error occur?
- If Err <> 0 Then
- WScript.Echo "Couldn't connect to " & strName
- Else
- 'execute WMI query
- Set colOS = objWMI.ExecQuery("SELECT ServicePackMajorVersion FROM Win32_OperatingSystem")
- 'go through each returned object
- For Each objOS In colOS
- WScript.Echo strName & " is on SP " & objOS.ServicePackMajorVersion
- Next
- End If
- 'turn off error trapping
- On Error GoTo 0
- End Sub
- Dim objFSO, objTS, strName
- Set objFSO = CreateObject("Scripting.FileSystemObject")
- Set objTS = objFSO.OpenTextFile(strFilename)
- Do Until objTS.AtEndOfStream
- strName = objTS.ReadLine
- WScript.Echo "Read " & strName & " from file…"
- DoObject strName
- Loop
- objTS.Close
Tags: list, list of computers, list of users, names, VBScript Scripts










