Script Center > Gallery > Scripting Techniques > Run Commands on Multiple Computers
TechNet Script Center logo

Welcome to the TechNet Script Center Gallery!

Each contribution is licensed to you under a License Agreement by its owner, not Microsoft. Microsoft does not guarantee the contribution or purport to grant rights to it.

Run Commands on Multiple Computers

(Community)
Rate it:
 
 
 
 
 
Script Code
VBScript
' Nino Nacarlo 12 Sept 2007
' ********************************
' This script will take a list of computers from a text file and run a command against each one. 
' (One computername on each line)
' Great way to deploy some out of hours. If you've got a large MSI you can use this to install it 
' on all the PCs left on over night then link the
' Group to the Group Policy to capture the ones left off the next time then turn the PC on
' Also great to make changes during the day to PCs that are switched ou
' *************************************
' To the run the command to the following scrip: cscript.exe \\locationToFile\Push.vbs 
' \\LocationOfTextFile.txt \\LocationOfBatchFile\MSIFile.bat \\LocationofPsexec.exe

' e.g. cscript.exe \\server\share$\Push.vbs \\server\share$\Pclist.txt \\server\share\psexec.exe

' If you take the ' before the wscript.echo on line 32 and 42 and add >> c:\results.txt to the end 
' on the command it will make a result log file

' e.g. cscript.exe \\server\share$\Push.vbs \\server\share$\Pclist.txt \\server\share\psexec.exe >> c:\results.txt
 
Const ForReading = 1
Wscript.echo " "
Wscript.echo " "
wscript.echo Wscript.Arguments(0)
wscript.echo Wscript.Arguments(1)
wscript.echo Wscript.Arguments(2)
Wscript.echo " "
Wscript.echo " "
Wscript.echo " "
'Opens the list of PCs to run the command on
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(Wscript.Arguments(0), ForReading)
 
'Loop though the list, Checks to see it the PC is reachable by the ping function, 
'then runs the command

Do Until objTextFile.AtEndOfStream
    strComputerTXT = objTextFile.ReadLine
    
If Reachable(strComputerTXT) Then
'WScript.Echo strComputerTXT & ", is Reachable! "
 
strArgument = Wscript.Arguments(2) & " \\" & strComputerTXT & " -u username -p password " & _
    chr(34) & Wscript.Arguments(1)  chr(34)
 
Set objShell = CreateObject("WScript.Shell")
objShell.Run strArgument
Wscript.Sleep 1000
Else
'WScript.Echo strComputerTXT & ", is Unreachable!"
End If
Loop

objTextFile.Close

Function Reachable(strComputer)
'     On Error Resume Next
 
 Dim objShell, objExec, strCmd, strTemp
 
 strCmd = "ping -n 1 " & strComputer
 
 Set objShell = CreateObject("WScript.Shell")
 Set objExec = objShell.Exec(strCmd)

 strTemp = UCase(objExec.StdOut.ReadAll)
 
 If InStr(strTemp, "REPLY FROM") Then
    Reachable = True 
 Else
    Reachable = False
 End If
End Function
Platforms
Windows Server 2008 R2 No
Windows Server 2008 No
Windows Server 2003 Yes
Windows 7 No
Windows Vista No
Windows XP Yes
Windows 2000 No
For online peer support, join The Official Scripting Guys Forum! To provide feedback or report bugs in sample scripts, please start a new discussion on the Discussions tab for this script.
Disclaimer The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.
Be the first to create a discussion.