Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Batch.vbs for Mass Computer Queries and Patches

Status
Not open for further replies.

CaffeineAddiciton

Technical User
Feb 15, 2005
5
US
This is a set of scripts that can be used for quickly doing tasks on massive ammouts of computers. This Program Structure works on a series of 3 files. The Batch.vbs, the Payload.vbs, and computers.txt. The First Script (Batch.vbs) reads the computerlist file (computers.txt) and uses the computer names in that file to run the Payload script (Payload.vbs) using the Computer name as an argument.

Code:
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::::::::::::::  Batch.vbs By CaffeineAddiction  :::::::::::::::::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
ComputerList = "computers.txt"
strScript = "PayLoad.vbs"
ScriptStartDelay = 200           ' set for 200 milliseconds
WaitForAllResponce = (3 * 60000) ' set for 3 min

Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")

':::::::::::::::::::::::::::::::::
'::::::   Payload Starter   ::::::
':::::::::::::::::::::::::::::::::
Set objCompList = fso.OpenTextFile(ComputerList, ForReading, True)
x = 0
On Error resume next
Do While objCompList.AtEndOfLine <> True
  strComputer = objCompList.ReadLine
  WshShell.Run strScript & " " & strComputer  , 0, False
  WScript.Sleep ScriptStartDelay
  x=x+1
Loop
objCompList.close
MsgBox "Finnished Running Payloads"
WScript.Sleep WaitForAllResponce
':::::::::::::::::::::::::::::::::
':::::   Querie Collector   ::::::
':::::::::::::::::::::::::::::::::
Set objResultsFile = fso.OpenTextFile("Results.txt", ForAppending, True)
Set objCompList = fso.OpenTextFile(ComputerList, ForReading, True)

Do While objCompList.AtEndOfLine <> True
  strComputerName = objCompList.ReadLine
  Set objCompFile = fso.OpenTextFile(strComputerName & ".tmp", ForReading, True)
  Do While objCompFile.AtEndOfLine <> True
    strCompInfo = objCompFile.ReadLine
    objResultsFile.WriteLine strCompInfo
  Loop
  objCompFile.Close
  fso.DeleteFile(strComputerName & ".tmp")
Loop
objResultsFile.Close
objCompList.Close

The Payload.vbs takes the argumets and uses them to open a .tmp file with the argument as the file name ... this
allows for the responces of multiple simultanious scripts to be collected with out getting write errors or having
some dirty code that makes the script wait untill a file is done being used by the other scripts. After all of the
computernames have been gone through, the Batch.vbs waits for a Specified ammout of time ... and reads each line
from the *.tmp files ... writes all of the contents to the resultes.txt and then deletes the *.tmp file (so you dont
have 1,000,000 files cluttering the folder you are working out of)

Code:
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::::::::::::::  Payload.vbs By CaffeineAddiction  :::::::::::::::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Set colArgs = WScript.Arguments

dim x
x = 0
dim switch(50)
For Each objArg in colArgs
  switch(x) = objArg
x = x + 1
Next
strComputer = switch(0)
if strComputer <> "" Then

LogFile = "LogFile.txt"
RecordFile = "Results.csv"

On Error resume next
Set fso = CreateObject("Scripting.FileSystemObject")
Set objResultsFile = fso.OpenTextFile(strComputer & ".tmp", ForAppending, True)
  Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

':::::::::::::::::::::::::::::::::::
'::::::::    Stuff to do    ::::::::
':::::::::::::::::::::::::::::::::::
'-=-=-=-= Find OS =-=-=-=-
    Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48)
    For Each objItem in colItems
      strOS = objItem.Caption
    Next

'-=-=-=-= Find MAC =-=-=-=-
    Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapter",,48)
    For Each objItem in colItems
    if objItem.Manufacturer <> "Microsoft" Then
      strMAC =  objItem.MACAddress
    End If
    Next

'-=-=-=-= Find IP =-=-=-=-
    Set colItems = objWMIService.ExecQuery("Select * from Win32_IP4RouteTable",,48)
    For Each objItem in colItems
      strIP = objItem.NextHop
    Next

'-=-=-=-= Comp Name =-=-=-=-
Set colItems = objWMIService.ExecQuery("Select * from Win32_MotherboardDevice",,48)
For Each objItem in colItems
    strCompName = objItem.SystemName
Next

'-=-=-=-= Write Info =-=-=-=-
    objResultsFile.WriteLine strComputer & "," & strCompName & "," & strIP & "," & strMAC & "," & strOS

objResultsFile.Close
End If

here is an example of the computer.txt. Computer names are seperated by lines ... new line = new computername
Code:
computer1
computer2
computer4
computer5
computer3

The beauty of this code is that the payload can be modified for diffrent uses ... you can either use it to do WMI Queries and query 50 computer at the same time ... or use it for patching and get it all done as fast as possible.

The only thing holding this script back, is the System spec of the comptuer you run it on ... if someone was creative enough ... they could modify this script so that it is all started and controlled by one script that runs itself on multiple diffrent comptuers
each using a unique computerlist ... thus allowing for tasks to be done at blazing speeds.

For those of you who want to do as little modifing as possible ... here is a template of the Payload.vbs with just the bare bones of the script ... aka add in your own taskings

Code:
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::::::::::::::::::::  Payload.vbs Template  :::::::::::::::::::::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Set colArgs = WScript.Arguments

dim x
x = 0
dim switch(50)
For Each objArg in colArgs
  switch(x) = objArg
x = x + 1
Next
strComputer = switch(0)
if strComputer <> "" Then

LogFile = "LogFile.txt"
RecordFile = "Results.csv"

On Error resume next
Set fso = CreateObject("Scripting.FileSystemObject")
Set objResultsFile = fso.OpenTextFile(strComputer & ".tmp", ForAppending, True)
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

':::::::::::::::::::::::::::::::::::
':::::   Start Editing Here    :::::
':::::::::::::::::::::::::::::::::::


'-=-=-=-= Write Query Info =-=-=-=-

'Here is an Example of output for making a comma delimited file:
'
'  objResultsFile.WriteLine strComputer & "," & strSomeVariable & "," & strEct & "," & strYou_get_the_idea
'


':::::::::::::::::::::::::::::::::::
':::::    Stop Editing Here    :::::
':::::::::::::::::::::::::::::::::::

objResultsFile.Close
End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top