none
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
                Next

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

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

    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.

    http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx

    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.

    http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/4912e441-a423-4bd9-99c1-e2e5df821741

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

    Cheers

    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.

    Regards,

    Salvador Manaois III
    MCITP | Enterprise & Server Administrator
    MCSE MCSA MCTS(x5) CIWA C|EH
    My Blog: Bytes and Badz 
    My Passion: My PhotoStream
    Monday, May 04, 2009 1:25 AM