SAPIEN Solutions

SAPIEN homepage
PrimalScript script editor+ide
PrimalScope script debugger
Free Tools script utilities
SAPIEN Press tech books
ScriptingAnswers.com learn+share
ScriptingOutpost.com online store
Blog.Sapien.com official blog
Contact Us

 

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.

  1. '
  2. ' ScriptingAnswers.com Essentials - by Don Jones
  3. '
  4. ' Targeting a list of computer
  5. ' This example shows how to read in a list of
  6. ' names (computer names, in this case), one at
  7. ' a time, and then do something with each name.
  8. ' You can easily add your own code to this
  9. ' template to make it more useful! Recommend
  10. ' running this from CScript as it produces a
  11. ' lot of output.

  12. Dim strFilename
  13. strFilename = "C:\computers.txt"

  14. Sub DoObject(strName)

  15. 'this is where YOUR code goes: strName will contain
  16. 'the current computer (or whatever) name. In this example,
  17. 'we'll use it to query the service pack number. Notice
  18. 'how the error trapping works.

  19. 'first, turn on error trapping
  20. On Error Resume Next

  21. 'try to make a WMI connection - note the use of the
  22. 'computer name from our strName variable
  23. Dim objWMI, colOS, objOS
  24. Set objWMI = GetObject("winmgmts:\\" & strName & "\root\cimv2")

  25. 'did an error occur?
  26. If Err <> 0 Then
  27. WScript.Echo "Couldn't connect to " & strName
  28. Else

  29. 'execute WMI query
  30. Set colOS = objWMI.ExecQuery("SELECT ServicePackMajorVersion FROM Win32_OperatingSystem")

  31. 'go through each returned object
  32. For Each objOS In colOS
  33. WScript.Echo strName & " is on SP " & objOS.ServicePackMajorVersion
  34. Next

  35. End If

  36. 'turn off error trapping
  37. On Error GoTo 0

  38. End Sub

  39. Dim objFSO, objTS, strName
  40. Set objFSO = CreateObject("Scripting.FileSystemObject")
  41. Set objTS = objFSO.OpenTextFile(strFilename)
  42. Do Until objTS.AtEndOfStream
  43. strName = objTS.ReadLine
  44. WScript.Echo "Read " & strName & " from file…"
  45. DoObject strName
  46. Loop
  47. objTS.Close
[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Tags: , , , ,

Leave a Reply


Entries (RSS) and Comments (RSS).