If you wish to run processes (programs) on the local machine or another machine, then I have
examples of WMI scripts that will get you started. However, if you are looking to kill a process then the .Terminate method is covered on a separate page.
Two more points before we start, firstly
this script deals with processes, I have a parallel script which deals with service. Secondly and disappointingly, while this script launches programs on a network machine, they only initialise in the
task manager and not as a GUI. It does however, launch the GUI on the local machine.
As part of a bigger script, perhaps you wish to stop, then re-start a process remotely. Alternatively,
you are scheduling a task and a process needs to run on a distant Windows server as a prerequisite of the main program executing.
There are numerous ways connect to other Windows computers on your
network. My
first choice would be Remote Desktop, however, if you want to automate a task, then a WMI script would be a better choice. Keep in mind that many Microsoft programs run as services, the good news is that
you can modify this strategy by substituting Win32_Service for Win32_Process.
Our first example launches a named process on the local machine (Windows Server
2003 or XP). For
example, execute Calc.exe. The key object is Win32_Process. VBScript will
manipulate Win32_Process to create an instance of our process or program.
Prerequisites for your WMI Script
You need a Window 2003 or XP machine for the .create method to execute. One obvious prerequisite is to check the name of the exe specified by
strExe. I have used Calc.exe. If necessary, research the program manually by using task manager to check the name of the process you wish to launch with your WMI script.
Instructions for Launching Processes WMI Script
Copy and paste the example script below into notepad or a VBScript editor.
Save the file with a .vbs extension, for example: ProcessLocal.vbs
Double click ProcessLocal.vbs and check that Calc.exe launches.
Script to Create (Start) a Process on the Local Computer
' StartProcessLocal.vbs ' Free example VBScript to start a process (not interactive) ' Author Guy Thomas http://computerperformance.co.uk/ ' Version 1.8 - December
2010 ' -------------------------------------------------------' Option Explicit Dim objWMIService, objProcess, objCalc Dim strShell, objProgram, strComputer, strExe
strComputer = "." strExe =
"Calc.exe" ' 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)
1) If you are experienced with WMI, then the two features to
home in on are, Win32_Service and objProcess.name.
2) If you are new to WMI then you will soon appreciate that all WMI scripts begin by employing winmgmts to access the root of the CIM library : Set objWMIService = GetObject("winmgmts:"
& strComputer & "\root\cimv2")
2) Perhaps you expected a {impersonationLevel=impersonate}! command? The reason that this statement is NOT needed is that the operating system
creates the process using your credentials and not those of the other machine.
3) Remember that Win32_Process is the key CIM class. What we are going to do is use the Win32_Process to create an instance of the command line. From there we will execute the program
specified by strExe.
From a VBScript perspective
4) There is a trick command on lines 18/19, pay careful note of the underscore(s). Set objProgram = objProcess.Methods_("Create").InParameters.SpawnInstance.
What make this item peculiar is that the .Methods_("Create" requires an underscore. This would be an error: .Methods("Create". What makes the whole construction tricky is that it needs to spread over 3
lines, and the first two lines need an underscore to make VBScript understand word-wrap.
Guy
Recommends: WMI Monitor and It's Free!
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft's operating systems. Fortunately, SolarWinds
have created a
Free WMI Monitor so that you can discover these gems of performance
information, and thus improve your scripts.
Take the guess work out of which WMI counters to use when scripting the
operating system, Active Directory or Exchange Server. Give this WMI monitor a
try - it's free.
Our mission is to launch a named process on a machine that we specify in strComputer. The key object is Win32_Process. VBScript will
manipulate Win32_Process to create an instance of our process or program.
Do be aware that there is one great disappointment with this technique, and that is the programs do not run interactively. Yes, you can see the process in task manager, but a console user would not see
the GUI program to the WMI process.
Prerequisites for your WMI Script
The crucial practical step is to set the NetBIOS name of the remote machine. You need a Window 2000 or later machine for the .create method to execute.
A remote XP machine would be ideal. One obvious piece of research is to run the program manually, then launch the task manager to check the name of the process you wish to launch with your WMI script.
Instructions for Launching Processes WMI Script
Copy and paste the example script below into notepad or a VBScript editor.
Save the file with a .vbs extension, for example: Process.vbs
Double click Process.vbs and enter the name of the machine where you want to launch the program.
' StartProcess.vbs ' Sample VBScript to start a process. Inputbox for name ' Author Guy Thomas http://computerperformance.co.uk/ ' Version 2.2 - December 2010
' -------------------------------------------------------' 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
Guy
Recommends: The Free Config Generator
SolarWinds' Config Generator is a free tool, which puts you in charge of
controlling changes to network routers and other SNMP devices.
Boost your network performance by activating network device features
you've already paid for.
Guy says that for newbies the biggest benefit of this free tool is that
it will provide the impetus for you to learn more about configuring the SNMP
service with its 'Traps' and 'Communities'. Try Config Generator now - it's
free!
1) If you are experienced with WMI, then the two features to
home in on are, Win32_Service and objProcess.name.
2) If you are new to WMI then you will soon appreciate that all WMI scripts begin by employing winmgmts to access the root of the CIM library : Set objWMIService = GetObject("winmgmts:"
& strComputer & "\root\cimv2")
2) Perhaps you expected a {impersonationLevel=impersonate}! command? The reason that this statement is NOT needed is that the operating system
creates the process using your credentials and not those of the other machine.
3) Remember that Win32_Process is the key CIM class. What we are going to do is use the Win32_Process to create an instance of the command line. From there we will execute the program
specified by strExe.
From a VBScript perspective
4) There is a trick command on lines 25/26, pay careful note of the underscore(s). Set objProgram = objProcess.Methods_("Create").InParameters.SpawnInstance.
What make this item peculiar is that the .Methods_("Create" requires an underscore. This would be an error: .Methods("Create". What makes the whole construction tricky is that it needs to spread over 3
lines, and the first two lines need an underscore to make VBScript understand word-wrap.
5) I added an input box to make you think about the machine of where you want to run the script. To
stop people entering nothing, I introduced a simple Do..Loop Until error correcting loop. As an aside, I like to combine as many VBScript techniques as practical.
Scripting processes gives you
control over remote machines. The key WMI object is Win32_Process. What the VBScript does is mimic you executing the process (program) from the command line. The result is that the named
process starts.
Windows Management Instrumentation (WMI) is
most useful for PowerShell scripting.
SolarWinds
have produced this
Free WMI Monitor to take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.