SWbemServices.ExecMethod 方法

SWbemServices物件的ExecMethod方法會執行方法提供者所匯出的方法。 這個方法會在轉送至適當提供者的方法執行時封鎖。 然後會傳回信息和狀態。 提供者,而不是 WMI,會實作 方法。

這個方法會在同步模式中呼叫。 如需詳細資訊,請參閱 呼叫方法

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

語法

objOutParams = .ExecMethod( _
  ByVal strObjectPath, _
  ByVal strMethodName, _
  [ ByVal objWbemInParams ], _
  [ ByVal iFlags ], _
  [ ByVal objWbemNamedValueSet ] _
)

參數

strObjectPath

必要。 字串,其中包含執行 方法之 物件的物件路徑。 如需詳細資訊,請參閱 描述 WMI 物件的位置

strMethodName

必要。 物件的方法名稱。

objWbemInParams [選擇性]

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

iFlags [選擇性]

保留的。 這個值必須為零。

objWbemNamedValueSet [選擇性]

一般而言,這是未定義的。 否則,這是 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 做為直接存取來執行 提供者方法 的替代方案。 ExecMethod方法可讓您取得輸出參數,如果提供者提供它們,則提供不支援輸出參數的指令碼語言。 否則,叫用方法的建議方法是使用直接存取。 如需詳細資訊,請參閱 操作類別和實例資訊

例如,在Win32_Service中呼叫StartService提供者方法的下列程式碼範例會使用直接存取。

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

此範例會呼叫 SWbemServices.ExecMethod 來執行 StartService 方法。 請注意,因為 SWbemServices.ExecMethod 尚未在物件上運作,所以需要物件路徑,與 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"

SWbemServices.ExecMethod方法需要物件路徑。 如果腳本已經保存 SWbemObject 物件,請使用 SWbemObject.ExecMethod 方法。

範例

下列範例顯示 ExecMethod 方法。 腳本會建立 Win32_Process 物件,代表執行記事本的進程。 它會顯示 InParameters 物件的設定,以及如何從 OutParameters 物件取得結果。 如需顯示非同步執行相同作業的腳本,請參閱 SWbemServices.ExecMethodAsync。 如需使用直接存取的範例,請參閱 在類別中建立方法Win32_Process。 如需使用 SWbemObject的相同作業範例,請參閱 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

規格需求

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

另請參閱

SWbemServices

SWbemObject.ExecMethod_

呼叫 Provider 方法

操作類別和實例資訊