Redigera

Dela via


SWbemObject.ExecMethod_ method

The ExecMethod_ method of the SWbemObject object executes a method exported by a method provider.

This method pauses 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.

For an explanation of this syntax, see Document Conventions for the Scripting API.

Syntax

objOutParams = .ExecMethod_( _
  ByVal strMethodName, _
  [ ByVal objwbemInParams ], _
  [ ByVal iFlags ], _
  [ ByVal objwbemNamedValueSet ] _
)

Parameters

strMethodName [in]

Required. Name of the method for the object.

objwbemInParams [in, optional]

This is 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 [in, optional]

Reserved and must be set to 0 (zero) if specified.

objwbemNamedValueSet [in, optional]

Typically, it 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.

Return value

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

Error codes

After the completion of the ExecMethod_ method, the Err object may contain one of the error codes in the following list.

wbemErrFailed - 2147749889 (0x80041001)

Unspecified error.

wbemErrInvalidClass - 2147749904 (0x80041010)

Specified class was not valid.

wbemErrInvalidParameter - 2147749896 (0x80041008)

A specified parameter is not valid.

wbemErrOutOfMemory - 2147749894 (0x80041006)

Not enough memory to complete the operation.

wbemErrInvalidMethod - 2147749934 (0x8004102E)

Requested method was not available.

wbemErrAccessDenied - 2147749891 (0x80041003)

Current user was not authorized to execute the method.

Remarks

This method is similar to SWbemServices.ExecMethod, but it operates directly on the object whose method is to be executed. For example, the following code example calls the StartService provider method in Win32_Service and uses direct access.

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

This version calls SWbemObject.ExecMethod_ to execute the StartService method.

oService = GetObject("winmgmts:Win32_Service=Alerter")
Set outParam = process.ExecMethod_("StartService")

Use SWbemObject.ExecMethod_ as an alternative to direct access for executing a provider method in cases where it is not possible to execute a method directly. For example, you would use SWbemObject.ExecMethod_ with a scripting language that does not support output parameters if your method has out parameters. Otherwise, the recommended means of invoking a method is to use direct access.

  • The SWbemObject.ExecMethod_ method assumes the object represented by SWbemObject contains the method to execute. By contrast, SWbemServices.ExecMethod requires an object path. Use SWbemObject.ExecMethod_ if you already have obtained the object whose method you want to execute.

Examples

The following example shows the ExecMethod method.The script creates a Win32_Process object representing a process running Notepad. For more information about a script illustrating the same operations performed asynchronously, see SWbemObject.ExecMethodAsync_. For an example using direct access, see Create Method in Class Win32_Process . For an example of the same operation using an SWbemServices object, see SWbemServices.ExecMethod.

' Connect to WMI and obtain a Win32_Process object.
' This is also an SWbemObject object.
Set oProcess = GetObject("winmgmts: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 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_

' Specify the name of the process to be run.
oInParams.CommandLine = "Notepad.exe"
Set oOutParams = oProcess.ExecMethod_("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

Requirement Value
Minimum supported client
Windows Vista
Minimum supported server
Windows Server 2008
Header
Wbemdisp.h
Type library
Wbemdisp.tlb
DLL
Wbemdisp.dll
CLSID
CLSID_SWbemObject
IID
IID_ISWbemObject

See also

SWbemObject

SWbemObject.ExecMethodAsync_

SWbemServices.ExecMethod