共用方式為


如何使用 WMI 執行異步 Configuration Manager 查詢

在 Configuration Manager 中,您會藉由呼叫 SWbemServices 物件 ExecQueryAsync 方法,以及實作接收方法來處理查詢結果,來執行 Configuration Manager 物件的同步查詢。

若要處理每個傳回的物件,請建立 objWbemSink.OnObjectReady 事件子程式。 若要在查詢完成時收到通知,請建立 objWbemSink.OnCompleted 事件子程式。

注意事項

異步查詢中不會傳回延遲屬性。 如需詳細資訊,請參閱 如何使用WMI讀取延遲屬性

執行異步查詢

  1. 設定與SMS提供者的連線。 如需詳細資訊,請參閱如何使用WMI 在 Configuration Manager 中聯機到SMS提供者

  2. 建立 OnObjectReady 子程式,以依查詢處理物件。

  3. 建立 OnCompleted 子程式來處理查詢完成。

  4. 使用您從步驟一取得的 SWbemServices 物件,使用 ExecQueryAsync 物件以異步方式查詢 Configuration Manager 物件。

範例

下列 VBScript 程式代碼範例會以異步方式查詢所有 SMS_Collection 物件。

如需呼叫範例程式代碼的相關信息,請參閱呼叫 Configuration Manager 代碼段

Dim bdone
Sub QueryCollection(connection)

    Dim sink
    bdone = False

    Set sink = WScript.CreateObject("wbemscripting.swbemsink","sink_")

    ' Query for all collections.
    connection.ExecQueryAsync sink, "select * from SMS_Collection"

    ' Wait until all instances are returned.
    While Not bdone
        wscript.sleep 1000
    Wend
 End Sub

' The sink subroutine to handle the OnObjectReady
' event. This is called as each object returns.
Sub sink_OnObjectReady(collection, octx)
    WScript.Echo "CollectionID: " + collection.CollectionID
    WScript.Echo "Name: " + collection.Name
    Wscript.Echo
End Sub

' The sink subroutine to handle the OnCompleted event.
' This is called when all the objects are returned.
' The oErr parameter obtains an SWbemLastError object,
' if available from the provider.
Sub sink_OnCompleted(HResult, oErr, oCtx)
    WScript.Echo "All collections returned"
    bdone = true
End Sub

此範例方法具有下列參數:

參數 Type 描述
connection SWbemServices SMS 提供者的有效連線。

另請參閱

Windows Management Instrumentation物件概觀如何使用 WMI 如何使用 WMI 如何使用 WMI 如何建立 Configuration Manager 物件,在 Configuration Manager 中使用 WMI 連線到 SMS 提供者來呼叫 Configuration Manager 物件類別方法使用 WMI 如何使用 WMI 如何使用 WMI 如何使用 WMI 執行同步 Configuration Manager 查詢來修改 Configuration Manager 物件來刪除 Configuration Manager 物件如何讀取 Configuration Manager物件使用 WMI如何使用 WMI 讀取延遲屬性Configuration Manager 擴充 WMI 查詢語言Configuration Manager 結果集Configuration Manager 查詢的特殊查詢