Execute process on Remote PC

From TomSchaefer.org Wiki

Jump to: navigation, search

Even need to run a batch on another computer remotely. How about install patches or even just configure an IP address. Here is a script that will allow you to execute a process on a remote PC, given that you have access.

 Option Explicit
 Dim objWMIService, objProcess
 Dim strShell, objProgram, strComputer, strExe, strInput
 strExe = "Calc.exe"
 ‘ Input Box to get name of machine to run the process
 Do
 strComputer = (InputBox(" ComputerName to Run Script",_
 "Computer Name"))
 If strComputer <> "" Then
 strInput = True
 End if
 Loop until strInput = True
 ‘ Connect to WMI
 set objWMIService = getobject("winmgmts://"_
 & strComputer & "/root/cimv2")
 ‘ Obtain the Win32_Process class of object.
 Set objProcess = objWMIService.Get("Win32_Process")
 Set objProgram = objProcess.Methods_( _
 "Create").InParameters.SpawnInstance_
 objProgram.CommandLine = strExe
‘Execute the program now at the command line.
 Set strShell = objWMIService.ExecMethod( _
 "Win32_Process", "Create", objProgram)
 WScript.echo "Created: " & strExe & " on " & strComputer
 WSCript.Quit
 ‘ End of Example of a Process VBScript

If you want you can change the script so that you can send the Process name and the PC via an Argument. Don’t forget to run is with cscript if you wish to have the Echo’s go to console.

Personal tools