Link to home
Start Free TrialLog in
Avatar of jason6215
jason6215

asked on

VB script to call list of machines

Hello,

I need the VB script to call a list of machines hook into the wmi and make directories on those remote machines.  I need to modify  the below script to call a list of machines instead of just one (strComputer = "XXXXXXDSK01"

strComputer = "XXXXXXDSK01"
Set objWMIService = GetObject _
    ("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
errReturn = objWMIService.Create _
    ("cmd.exe /c md c:\windows\mediascripts", Null, Null, intProcessID)
Avatar of danengle
danengle

Here ya go.  will read in a text file as a command-line parameter.
'==========================================================================
'
' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 2007
'
' NAME: 
'
' AUTHOR:  , 
' DATE  : 8/12/2008
'
' COMMENT: 
'
'==========================================================================
Dim objFSO, objFile, strContents, arrContents
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
if WScript.Arguments(0) = Null Then 
	WScript.Echo "Usage:  Supply a list of machines to create the directory"
ElseIf objFSO.FileExists (WScript.Arguments(0)) Then
 
	Set objFile = objFSO.GetFile(wscript.Arguments(0))
	If objFile.Size > 0 Then
	    Set objReadFile = objFSO.OpenTextFile(WScript.Arguments(0), 1)
	    strContents = objReadFile.ReadAll	  
	    objReadFile.Close
	    arrContents = Split(strContents, VbCrLf)
	    WScript.Echo UBound(arrContents)
	    For ctr = 0 To UBound(arrContents)
		    If arrContents(ctr) <> "" Then 
		    WScript.Echo "str:" & arrContents(ctr)
		    	If WMIPingBox(arrContents(ctr)) = True Then	
		    		MakeDir arrContents(ctr)
		    	Else 
		    		WScript.Echo arrContents(ctr) & ":Offline"
		    	End If
		    End if
	    Next
	    
	Else
	    Wscript.Echo "The file is empty."
	    WScript.quit
	End If
End If
 
function MakeDir (strComputer)
    dim objWMIService, errReturn
    Set objWMIService = GetObject ("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
    errReturn = objWMIService.Create _
    ("cmd.exe /c md c:\windows\mediascripts", Null, Null, intProcessID)
    set objWMIService = Nothing
End Function
 
Function WMIPingBox(PC) ' pings a machine to see if it's online.
	Dim oPing,oWMI
	Dim o
 
	Set oPing = GetObject("winmgmts:").ExecQuery("Select * from Win32_PingStatus where statusCode = 0 and address = '"& PC &"'")
	For Each o In oPing
		If PC = o.Address Then
			WMIPingBox = True
			Set oPing = Nothing
			Exit Function
		End If
	Next
	WMIPingBox = False
	Set oPing = Nothing
	Exit Function
		
End Function

Open in new window

didn't use option explicit.  Didn't change behavior, but not good practice.  Here's the updated script.
Option Explicit
Dim objFSO, objFile, strContents, arrContents, objReadFile, ctr
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
if WScript.Arguments(0) = Null Then 
	WScript.Echo "Usage:  Supply a list of machines to create the directory"
ElseIf objFSO.FileExists (WScript.Arguments(0)) Then
 
	Set objFile = objFSO.GetFile(wscript.Arguments(0))
	If objFile.Size > 0 Then
	    Set objReadFile = objFSO.OpenTextFile(WScript.Arguments(0), 1)
	    strContents = objReadFile.ReadAll	  
	    objReadFile.Close
	    arrContents = Split(strContents, VbCrLf)
	    WScript.Echo UBound(arrContents)
	    For ctr = 0 To UBound(arrContents)
		    If arrContents(ctr) <> "" Then 
		    WScript.Echo "str:" & arrContents(ctr)
		    	If WMIPingBox(arrContents(ctr)) = True Then	
		    		MakeDir arrContents(ctr)
		    	Else 
		    		WScript.Echo arrContents(ctr) & ":Offline"
		    	End If
		    End if
	    Next
	    
	Else
	    Wscript.Echo "The file is empty."
	    WScript.quit
	End If
End If
 
function MakeDir (strComputer)
    dim objWMIService, errReturn, intProcessID
    Set objWMIService = GetObject ("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
    errReturn = objWMIService.Create _
    ("cmd.exe /c md c:\windows\mediascripts", Null, Null, intProcessID)
    set objWMIService = Nothing
End Function
 
Function WMIPingBox(PC) ' pings a machine to see if it's online.
	Dim oPing,oWMI
	Dim o
 
	Set oPing = GetObject("winmgmts:").ExecQuery("Select * from Win32_PingStatus where statusCode = 0 and address = '"& PC &"'")
	For Each o In oPing
		If PC = o.Address Then
			WMIPingBox = True
			Set oPing = Nothing
			Exit Function
		End If
	Next
	WMIPingBox = False
	Set oPing = Nothing
	Exit Function
		
End Function

Open in new window

Avatar of jason6215

ASKER

Thanks,

Im getting Line: 6
Begining with the If
Subscript out of range
Avatar of RobSampson
Hi, change this
if WScript.Arguments(0) = Null Then

to this
if WScript.Arguments.Count = 0 Then

Regards,

Rob.
Thanks, where do you define the text file of the list of machines in the script?

When I run the script it just echos "Usage:  Supply a list of machines to create the directory"
you supply it when running it from a cmd prompt (or batch file, etc).

from a cmd prompt, you can run it as:
cscript.exe yourscript.vbs c:\listofmachines.txt

where "yourscript.vbs" is whatever name you saved this script.
ASKER CERTIFIED SOLUTION
Avatar of danengle
danengle

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
it will also echo out any errors in creating the directory.  you can capture the output with the ">".  

ex:  cscript.exe yourscript.vbs > c:\results.txt