Run a program against multiple computers

    General discussion

  • Hello,

    I'm new to scripting, what I am trying to create is a simple script that will execute a file on the C: drive of remote computers.  The script will do the following:

    Ping all computers in an IP range to make sure the computer is on the network.
    Select and connect to each computer.
    Execute a batch file on the remote computer's C: drive.

    I found the following template which is exactly what I'm looking for.  My question is at the line "Insert your code here" do I simply insert a command that run the executable for eample.

    errReturn = objWMIService.Create ("C:\200XP.bat", Null, Null, intProcessID)  I do not fully understand what all of the lines do.  I'm just making sure the script will do what it should do first, so I could run it on a test environment of few computer in a specified IP range. Your response is much appricated.

    Start of the script>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    On Error Resume Next

    intStartingAddress = 1
    intEndingAddress = 100
    strSubnet = "192.168.1."  (replace IP range with actual IP range)

    For i = intStartingAddress to intEndingAddress
        strComputer = strSubnet & i

        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

                ' =====================================================================
                ' Insert your code here
                ' =====================================================================

                Set objWMIService = GetObject _
                    ("winmgmts:\\" & strComputer & "\root\cimv2")
                Set colItems = objWMIService.ExecQuery _
                    ("Select * From Win32_OperatingSystem")
                For Each objItem in ColItems
                    Wscript.Echo strComputer & ": " & objItem.Caption

                ' =====================================================================
                ' End
                ' =====================================================================

                Wscript.Echo strComputer & " could not be reached."
            End If

    Sunday, May 03, 2009 1:28 AM

All replies

  • Hi

    I would advise using the sysinternals tool "psexec" for this. You can download it here.

    You can also view a previous post of mine to get the code for reading a list of hostnames, IP Addresses or DNS names from a text file and verifying that they respond to a ping. You would then need to modify this to initiate psexec to start the batch file or script on each remote system that responded to the ping.

    Give that a try and if you get stuck reply back and i'll code something up for you. Hope that helps


    Matt :)
    Sunday, May 03, 2009 2:33 AM
  • I appreciate your suggestions Matt.  For me, part of doing this was to learn scripting.  I will need to do much more reading to fully understand some of the more complex scripts.  Right now, I enjoy writing very simple scripts for running against a single computer. 

    Monday, May 04, 2009 1:08 AM
  • Hi,

    Matt is right; you can make use of Sysinternals' psexec to launch the batch file on the remote machine. Alternatively, you can make use of the SCHTASKS command (or AT) to launch the batch file. For example, to launch run-notepad.bat on the remote machine, try inserting this code into your script:

    strRunBacth = "%comspec% /c schtasks /S " & strComputer & " /ST 12:00 /SC Once /TR notepad"
    Set objExecObject = objShell.Exec(strCommand)

    You can change the start time (/ST) to any value you require.


    Salvador Manaois III
    MCITP | Enterprise & Server Administrator
    My Blog: Bytes and Badz 
    My Passion: My PhotoStream
    Monday, May 04, 2009 1:25 AM