SWbemObject.ExecMethodAsync_ 方法

SWbemObject的 ExecMethodAsync_方法會以非同步方式執行方法提供者匯出的方法。 這個方法類似于 SWbemServices.ExecMethodAsync,但會直接在要執行的方法物件上操作。 Windows Management Instrumentation (WMI) 不會實作此方法。 提供者會實作這個方法。

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

語法

objOutParams = .ExecMethodAsync_( _
  ByVal objWbemSink, _
  ByVal strMethodName, _
  [ ByVal objwbemInParams ], _
  [ ByVal iFlags ], _
  [ ByVal objwbemNamedValueSet ], _
  [ ByVal objWbemAsyncContext ] _
)

參數

objWbemSink [in]

必要。 這是接收方法呼叫結果的物件接收。 輸出參數會傳送至所提供物件接收的 SWbemSink.OnObjectReady 事件。 呼叫機制的結果會傳送至所提供物件接收的 SWbemSink.OnCompleted 事件。 請注意 ,SWbemSink.OnCompleted 不會接收方法的傳回碼。 不過,它會收到實際呼叫傳回機制的傳回碼,而且只適用于驗證呼叫是否發生,或因機械原因而失敗。 從 方法傳回的結果碼會在提供給 SWbemSink.OnObjectReady的輸出參數物件中傳回。 如果傳回任何錯誤碼,則不會使用提供的 IWbemObjectSink 物件。 如果呼叫成功,則會呼叫使用者的 IWbemObjectSink 實作來指出作業的結果。

strMethodName [in]

必要。 這是 物件的方法名稱。

objwbemInParams [in, optional]

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

iFlags [in, optional]

決定呼叫行為的整數。 此參數可以接受下列值。

wbemFlagSendStatus (128 (0x80) )

導致非同步呼叫將狀態更新傳送至物件接收的 SWbemSink.OnProgress 事件處理常式。

wbemFlagDontSendStatus (0 (0x0) )

防止非同步呼叫將狀態更新傳送至物件接收的 OnProgress 事件處理常式。

objwbemNamedValueSet [in, optional]

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

objWbemAsyncCoNtext [in, optional]

這是 SWbemNamedValueSet 物件,會傳回物件接收,以識別原始非同步呼叫的來源。 如果您要使用相同的物件接收進行多個非同步呼叫,請使用此參數。 若要使用此參數,請建立 SWbemNamedValueSet 物件,並使用 SWbemNamedValueSet.Add 方法來新增值,以識別您進行的非同步呼叫。 這個 SWbemNamedValueSet 物件會傳回至物件接收,而且可以使用 SWbemNamedValueSet.Item 方法擷取呼叫的來源。 如需詳細資訊,請參閱 呼叫方法

傳回值

這個方法沒有傳回值。 如果呼叫成功, 則 OutParameters 物件也是 SWbemObject 物件,會提供給 objWbemSink中指定的接收。 傳回的 OutParameters 物件包含所執行方法的輸出參數和傳回值。

錯誤碼

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

wbemErrFailed - 2147749889 (0x80041001)

未指定的錯誤。

wbemErrInvalidClass - 2147749904 (0x80041010)

指定的類別無效。

wbemErrInvalidParameter - 2147749896 (0x80041008)

指定的參數無效。

wbemErrOutOfMemory - 2147749894 (0x80041006)

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

wbemErrInvalidMethod - 2147749934 (0x8004102E)

要求的方法無法使用。

wbemErrAccessDenied - 2147749891 (0x80041003)

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

備註

當您無法直接執行方法時,請使用 SWbemObject.ExecMethodAsync_ 方法做為直接存取權,以執行 提供者方法 。 例如,如果您的方法具有 out 參數,請使用 SWbemObject.ExecMethodAsync_ 方法搭配不支援輸出參數的指令碼語言。 否則,建議您使用直接存取叫用方法。 如需詳細資訊,請參閱 操作類別和實例資訊

此呼叫會立即傳回。 要求的物件和狀態會透過傳遞至 objWbemSink中指定的接收回呼傳回給呼叫端。 若要在抵達時處理每個物件,請建立 objWbemSinkOnObjectReady 事件副程式。 傳回所有物件之後,您可以在 objWbemSink實作中執行最終處理。OnCompleted 事件。

非同步回呼可讓未驗證的使用者將資料提供給接收。 這對您的腳本和應用程式造成安全性風險。 若要消除風險,請使用半同步通訊或同步通訊。 如需詳細資訊,請參閱 呼叫方法

如果執行的方法具有輸入參數,則必須建構 InParameters 物件和 objWbemInParamInParam 參數,如 建構 InParameters 物件剖析 OutParameters 物件中所述。

SWbemObject.ExecMethodAsync_方法假設SWbemObject所代表的物件包含要執行的方法。 SWbemServices.ExecMethodAsync方法需要物件路徑。

範例

下列範例顯示 ExecMethodAsync 方法。 腳本會建立 Win32_Process 物件,代表執行記事本的進程。 它會顯示 InParameters 物件的設定,以及如何從 OutParameters 物件取得結果。

如需顯示同步執行之相同作業的腳本,請參閱 SWbemObject.ExecMethod。 如需使用直接存取的範例,請參閱 在類別中建立方法Win32_Process。 如需使用 SWbemServices 物件之相同作業的範例,請參閱 SWbemServices.ExecMethodAsync

On Error Resume Next

'Get a Win32_Process class description
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"

' Create a sink to receive event resulting
' from the ExecMethodAsync call.
Set Sink = wscript.CreateObject( _
    "WbemScripting.SWbemSink", "Sink_")

' Call the Win32_Process.Create method asynchronously. Set up a 
' wait so the results of the method execution can be returned to 
' the sink subroutines.
bDone = false

' Call SWbemObject.ExecMethodAsync on the oProcess object.
oProcess.ExecMethodAsync_ Sink, "Create", oInParams, 0 
' 
while not bDone
    wscript.sleep 1000
wend

' Sink subroutines
sub Sink_OnObjectReady(oOutParams, oContext)
    wscript.echo "Sink_OnObjectReady subroutine " _
    & VBNewLine & "ReturnValue = " _
    & oOutParams.ReturnValue & VBNewLine & _
    "ProcessId = " & oOutParams.ProcessId
end sub

sub Sink_OnCompleted(HResult, LastErrorObj, oContext)
    wscript.echo "Sink_OnCompleted subroutine, hresult = " _
    & hex(HResult)
    bdone = true
end sub

規格需求

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

另請參閱

SWbemObject

SWbemServices.ExecMethodAsync