Introduction to Creating (Start) a Process with WMIIf 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. Topics for Creating a Process with WMI
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
2005 ' -------------------------------------------------------' 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) WScript.echo "Created: " & strExe & " on " & strComputer WSCript.Quit ' End of free example of a Process VBScript
From a WMI perspective 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. 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.
Script to Create (Start) a Process on a Computer
' StartProcess.vbs ' Sample VBScript to start a process. Inputbox for name ' Author Guy Thomas http://computerperformance.co.uk/ ' Version 2.2 - December 2005
' -------------------------------------------------------' 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
WMI Tutorial - Learning PointsFrom a WMI perspective 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.
Their topics and material are ideal for getting you started with VBScript. The
videos are easy to follow and you can control the pace. Try their free demo material and then see if you want to buy the full package.
See more about VB Script Training CD.
See Also
|