Creating Processes

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

The WMI Win32_Process Create method allows you to create processes on either the local computer or a remote computer. This is similar to Windows Script Host (WSH) version 5.6, which also allows you to create processes on either the local computer or a remote computer (using the WSHController object).

For the most part, WSH provides a simpler approach for starting processes on the local computer. For example, these two lines represent all the code needed to start a hypothetical application named Database.exe:

Set objShell = CreateObject("Wscript.Shell") objShell.Run "database.exe" 

When you need to create a process on remote computers, however, Win32_Process Create has the following advantages:

  • The Win32_Process Create method works on any computer running any version of WMI. By contrast, WSH requires you to configure both computers to allow for the use of the WSHController object, and it works only if WSH 5.6 has been installed on both computers.
  • You can create the process using a single script. WSH, by contrast, needs two scripts to start a remote process: a script on the remote computer that starts the process and a script on the local computer that starts the remote script.
  • You can retrieve the process ID for the created process; this allows you to monitor the process and, if necessary, terminate it. By contrast, WSH can monitor and terminate only the script that launched the new process, not the process itself.
  • You can specify additional options (such as priority and window style) when starting an application.

Scripting Steps

Listing 14.10 contains a script that creates a process on a remote computer. To carry out this task the script must perform the following steps:

  1. Create a variable to specify the computer name.
  2. Use a GetObject call to connect to the WMI namespace root\cimv2 on the computer, and set the impersonation level to "impersonate."

    By specifying the class name as part of the call, the script is connected directly to the Win32_Process class on the remote computer.

  3. Use the Create method to create a new process on the remote computer, and specify the following parameters:
    • Database.exe. Name of the executable file to be started. If the file is in the computer s path, only the name must be specified; otherwise, you must specify the entire path name relative to the computer where the process runs. To start a script remotely, as opposed to an executable file, specify the script host as part of the name (for example, "Wscript.exe MyScript.vbs").
    • USE_SAME_STARTUP_FOLDER. Constant indicating the startup folder for the new process. The value Null is assigned to this constant to indicate that the new process should use the same startup folder as the script that created the process. This parameter is typically used for command-line applications and scripts that must reference a particular drive or folder when starting.
    • NO_STARTUP_OPTIONS. Constant indicating that there are no special startup options for the process. For more information about these options, see Modifying Process Startup Options later in this chapter.
    • intProcessID. If the Create method succeeds, the value of this variable is set to the process ID assigned to the remote process.
  4. Return an error code of 0 if the process is successfully created, and echo the process ID assigned to the new process.
  5. Return an error code other than 0 if the process could not be created, and echo that error code.

Listing 14.10   Creating a Process on a Remote Computer

1 2 3 4 5 6 7 8 9 10 11 12 
strComputer = "webserver" Set objWMIService = GetObject("winmgmts:" _     & "{impersonationLevel=impersonate}!\\" & strComputer & _         "\root\cimv2:Win32_Process") errReturn = objWMIService.Create("database.exe", null, null, intProcessID) If errReturn = 0 Then     Wscript.Echo "Database.exe was started with a process ID of " _          & intProcessID & "." Else     Wscript.Echo "Database.exe could not be started due to error " & _         errReturn & "." End If

send us your feedback Send us your feedback « Previous | Next »   


Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 635

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net