SWbemObject.ExecMethod_ 方法

SWbemObject物件的ExecMethod_方法會執行方法提供者所匯出的方法。

這個方法會在轉送至適當提供者的方法執行時暫停。 然後會傳回信息和狀態。 提供者而非 WMI 會實作 方法。

如需此語法的說明,請參閱 腳本 API 的檔慣例

語法

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

參數

strMethodName [in]

必要。 物件的方法名稱。

objwbemInParams [in, optional]

這是 SWbemObject 物件,其中包含所執行方法的輸入參數。 根據預設,這個參數是未定義的。 如需詳細資訊,請參閱 建構 InParameters 物件和剖析 OutParameters 物件

iFlags [in, optional]

如果指定,則保留且必須設定為 0 (零) 。

objwbemNamedValueSet [in, optional]

一般而言,它是未定義的。 否則,這是 SWbemNamedValueSet 物件,其元素代表服務要求的提供者可以使用的內容資訊。 支援或需要這類資訊的提供者必須記載已辨識的值名稱、值的資料類型、允許的值和語意。

傳回值

如果這個方法成功, SWbemObject 物件會傳回。 傳回的物件包含所執行方法的 out 參數和傳回值。

錯誤碼

完成 ExecMethod_ 方法之後, Err 物件可能會包含下列清單中的其中一個錯誤碼。

wbemErrFailed - 2147749889 (0x80041001)

未指定的錯誤。

wbemErrInvalidClass - 2147749904 (0x80041010)

指定的類別無效。

wbemErrInvalidParameter - 2147749896 (0x80041008)

指定的參數無效。

wbemErrOutOfMemory - 2147749894 (0x80041006)

記憶體不足,無法完成作業。

wbemErrInvalidMethod - 2147749934 (0x8004102E)

要求的方法無法使用。

wbemErrAccessDenied - 2147749891 (0x80041003)

目前使用者未獲授權執行 方法。

備註

這個方法類似于 SWbemServices.ExecMethod,但它會直接在方法要執行的物件上運作。 例如,下列程式碼範例會呼叫 Win32_Service中的StartService提供者方法,並使用直接存取

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

此版本會 呼叫 SWbemObject.ExecMethod_ 來執行 StartService 方法。

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

在無法直接執行方法的情況下,請使用 SWbemObject.ExecMethod_ 作為直接存取來執行 提供者方法 的替代方案。 例如,如果您的方法有 out 參數,您可以使用 SWbemObject.ExecMethod_ 與不支援輸出參數的指令碼語言。 否則,叫用方法的建議方法是使用直接存取。

  • SWbemObject.ExecMethod_方法假設SWbemObject所代表的物件包含要執行的方法。 相反地, SWbemServices.ExecMethod 需要物件路徑。 如果您已經取得想要執行其方法的物件,請使用 SWbemObject.ExecMethod_

範例

下列範例顯示 ExecMethod 方法。腳本會建立 Win32_Process 物件,代表執行記事本的進程。 如需說明非同步執行相同作業的腳本詳細資訊,請參閱 SWbemObject.ExecMethodAsync_。 如需使用直接存取的範例,請參閱 類別中的 Create 方法 Win32_Process 。 如需使用 SWbemServices 物件之相同作業的範例,請參閱 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

規格需求

需求
最低支援的用戶端
Windows Vista
最低支援的伺服器
Windows Server 2008
標頭
Wbemdisp.h
類型程式庫
Wbemdisp.tlb
DLL
Wbemdisp.dll
CLSID
CLSID_SWbemObject
IID
IID_ISWbemObject

另請參閱

SWbemObject

SWbemObject.ExecMethodAsync_

SWbemServices.ExecMethod