WMI Scripts

WMI - Creating (Start) a Process

VBScript Start Process with WMI

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.

Topics for Creating a Process with WMI

 ♣

Scenario - Why Start a Process on a Remote 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.

Example 1 - Start Process on the Local Machine

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

  1. Copy and paste the example script below into notepad or a VBScript editor.
  2. Save the file with a .vbs extension, for example: ProcessLocal.vbs 
  3. 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)

WScript.echo "Created: " & strExe & " on " & strComputer
WSCript.Quit

' End of free example of a Process VBScript

WMI VBScrpt Start Service Tutorial - Learning Points

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.

Guy Recommends: WMI Monitor and It's Free!Solarwinds Free WMI Monitor

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.

Download your free copy of WMI Monitor

Example 2 - Start Process on a Remote Machine

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

  1. Copy and paste the example script below into notepad or a VBScript editor.
  2. Save the file with a .vbs extension, for example: Process.vbs 
  3. Double click Process.vbs and enter the name of the machine where you want to launch the program.

See how to set process priority in Windows 8 »

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

Solarwinds Config GeneratorGuy 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!

Download your free copy of Config Generator

WMI Tutorial - Learning Points

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 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.

Summary of Creating (Start) a Process

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.

See more VBScript process examples:

VBScript List Process   • VBscript Start Process   VBScript Stop Process

WMI Home   • VBScript Services   • PowerShell Processes   • PowerShell Stop Process

WMI Home   • WMI Win32   • VBScript Echo   • WMI VBScript   • Downlad Free WMI Monitor

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor for PowershellSolarwinds WMI Monitor

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.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2014 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: