分享方式:


如何使用 Managed 程式碼讀取延遲屬性

若要從查詢中傳回的Configuration Manager物件讀取延遲屬性,您可以取得 物件實例,它會從 SMS 提供者擷取任何延遲物件屬性。

注意事項

如果您知道 WMI 物件的完整路徑,對 GetInstance 方法的呼叫會傳回 WMI 物件以及任何延遲屬性。 如需詳細資訊,請參閱如何使用 Managed 程式碼讀取Configuration Manager物件

如需詳細資訊,請參閱Configuration Manager延遲屬性。

讀取延遲屬性

  1. 設定與 SMS 提供者的連線。 如需詳細資訊,請參閱How to Connect to an SMS Provider in Configuration Manager by Using Managed Code

  2. 使用QueryProcessor物件來查詢Configuration Manager物件。

  3. 逐一查看查詢結果。

  4. 使用您在第一個步驟中取得的 WqlConnectionManager ,呼叫 GetInstance 以取得您想要從中取得延遲屬性之每個查詢物件的 IResultObject 物件。

範例

下列 C# 程式碼範例會查詢所有 SMS_Collection 物件,然後顯示從 lazy 屬性取得的 CollectionRules 規則名稱。

如需呼叫範例程式碼的相關資訊,請參閱呼叫Configuration Manager程式碼片段

public void ReadLazyProperty(WqlConnectionManager connection)  
{  
    try  
    {  
        // Query all collections.  
        IResultObject collections = connection.QueryProcessor.ExecuteQuery("Select * from SMS_Collection");  
        foreach (IResultObject collection in collections)  
        {  
            // Get the collection object and lazy properties.  
            collection.Get();  

            Console.WriteLine(collection["Name"].StringValue);  

            // Get the rules.  
            List<IResultObject> rules = collection.GetArrayItems("CollectionRules");  
            if (rules.Count == 0)  
            {  
                Console.WriteLine("No rules");  
                Console.WriteLine();  
                continue;  
            }  

            foreach (IResultObject rule in rules)  
            {  
                // Display rule names.  
                Console.WriteLine("Rule name: " + rule["RuleName"].StringValue);  
            }  

            Console.WriteLine();  
        }  
    }  
    catch (SmsQueryException ex)  
    {  
        Console.WriteLine("Failed to get collection. Error: " + ex.Message);  
        throw;  
    }  
}  

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

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

正在編譯程式碼

命名空間

系統

System.Collections.Generic

System.ComponentModel

Microsoft。ConfigurationManagement.ManagementProvider

Microsoft。ConfigurationManagement.ManagementProvider.WqlQueryEngine

組件

microsoft.configurationmanagement.managementprovider

adminui.wqlqueryengine

健全的程式設計

可引發的Configuration Manager例外狀況為SmsConnectionExceptionSmsQueryException。 這些可以與 SmsException一起攔截。

另請參閱

物件概觀Configuration Manager延遲屬性
如何使用 Managed 程式碼呼叫Configuration Manager物件類別方法
如何使用 Managed 程式碼連線到Configuration Manager提供者
如何使用 Managed 程式碼建立Configuration Manager物件
如何使用 Managed 程式碼修改Configuration Manager物件
如何使用 Managed 程式碼執行非同步Configuration Manager查詢
如何使用 Managed 程式碼執行同步Configuration Manager查詢
如何使用 Managed 程式碼讀取Configuration Manager物件