SWbemServices.ExecMethod Method

The ExecMethod method of the SWbemServices object executes a method that is exported by a method provider. This method blocks while the method that is forwarded to the appropriate provider executes. The information and status are then returned. The provider, rather than WMI, implements the method.

This method is called in the synchronous mode. For more information, see Calling a Method.

The following syntax is language-neutral. For an explanation of this syntax, see Document Conventions for the Scripting API.

Syntax

Script

objOutParams = ExecMethod(
  strObjectPath,
  strMethodName,
  [ objWbemInParams = null ],
  [ iFlags = 0 ],
  [ objWbemNamedValueSet = null ]
)

Parameters

strObjectPath

Required. String that contains the object path of the object for which the method is executed. For more information, see Describing the Location of a WMI Object.

strMethodName

Required. Name of the method for the object.

objWbemInParams [optional]

An SWbemObject object that contains the input parameters for the method being executed. By default, this parameter is undefined. For more information, see Constructing InParameters Objects and Parsing OutParameters Objects.

iFlags [optional]

Reserved. This value must be zero.

objWbemNamedValueSet [optional]

Typically, this is undefined. Otherwise, this is an SWbemNamedValueSet object whose elements represent the context information that can be used by the provider that is servicing the request. A provider that supports or requires such information must document the recognized value names, data type of the value, allowed values, and semantics.

objOutParams

If the method is successful, an SWbemObject object is returned. The returned object contains the out parameters and return value for the method that is being executed.

Return Value

This method does not return a value.

Remarks

Use SWbemServices.ExecMethod as an alternative to direct access for executing a provider method in cases where it is not possible to execute a method directly. The ExecMethod method allows you to obtain output parameters, if the provider supplies them, with a scripting language such as JScript that does not support output parameters. Otherwise, the recommended means of invoking a method is to use direct access. For more information, see Manipulating Class and Instance Information.

For example, the following code example that calls the StartService provider method in Win32_Service uses direct access.

oService = GetObject("winmgmts:Win32_Service=Alerter")
iStatus = oService.StartService()

This example calls SWbemServices.ExecMethod to execute the StartService method. Note that an object path is required because SWbemServices.ExecMethod is not operating on the object already, unlike SWbemObject.ExecMethod.

Set WbemServices = GetObject("winmgmts:")
Set oService = GetObject("winmgmts:Win32_Service='Alerter'")
Set oPath = GetObject("winmgmts:Win32_Service='Alerter'").Path_
WbemServices.ExecMethod oPath, "StartService"

The SWbemServices.ExecMethod method requires an object path. If the script already holds an SWbemObject object, use the SWbemObject.ExecMethod method.

Examples

For script code examples, see WMI Tasks for Scripts and Applications and the TechNet ScriptCenter Script Repository.

The following example shows the ExecMethod method. The script creates a Win32_Process object that represents a process that is running Notepad. It shows the setting up of an InParameters object and how to obtain results from an OutParameters object. For a script that shows the same operations performed asynchronously, see SWbemServices.ExecMethodAsync. For an example of using direct access, see Create Method in Class Win32_Process. For an example of the same operation using an SWbemObject, see SWbemObject.ExecMethod.

' Connect to WMI
set Services = getobject("winmgmts:root\cimv2")

' Obtain the class definition object of a Win32_Process object.
Set oProcess = Services.Get("Win32_Process")

' Create the SWbemMethod.InParameters object
' to hold the input parameter needed
' for the Win32_Process.Create method call.
' The oProcess.Methods_("Create") call
' obtains obtains a class object that
' defines the correct input parameters
' for the Win32_Process.Create call.
' The InParameters object is an 
' SWbemObject object so SWbemObject.SpawnInstance_ 
'can be called to create it.

Set oInParams = _
    oProcess.Methods_("Create").InParameters.SpawnInstance_
oInParams.CommandLine = "Notepad.exe"

'Call SWbemServices.ExecMethod with the WMI path Win32_Process
Set oOutParams = _
    Services.ExecMethod( "Win32_Process", "Create", oInParams)

If oOutParams.ReturnValue = 0 Then
    wscript.echo "Create method executed successfully."
Else
' If the Create method failed to execute,
' an empty OutParameters object is returned. 
    If IsNull(oOutParams.ReturnValue) Then
        wscript.echo "Create method failed to execute."  
    Else
        wscript.echo "Create method executed" _
            & " but had error " _
            & "0x" & hex(oOutParams.ReturnValue)
    End If
End If

Requirements

Minimum supported clientWindows 2000 Professional
Minimum supported serverWindows 2000 Server
Type LibraryWbemdisp.tlb
DLLWbemdisp.dll

See Also

SWbemObject.ExecMethod_
Calling a Provider Method
Manipulating Class and Instance Information

Send comments about this topic to Microsoft

Build date: 11/3/2009

Tags :


Page view tracker