若要從查詢中傳回的 Configuration Manager 物件讀取延遲屬性,您可以取得 對象實例,它會從 SMS 提供者擷取任何延遲物件屬性。
注意事項
如果您知道 WMI 物件的完整路徑,對 GetInstance 方法的呼叫會傳回 WMI 物件以及任何延遲屬性。 如需詳細資訊,請參閱如何使用Managed程式碼讀取 Configuration Manager物件。
如需詳細資訊,請參閱 Configuration Manager 延遲屬性。
讀取延遲屬性
設定與SMS提供者的連線。 如需詳細資訊,請參閱 How to Connect to an SMS Provider in Configuration Manager by Using Managed Code。
使用 QueryProcessor 物件來查詢 Configuration Manager 物件。
逐一查看查詢結果。
使用您在第一個步驟中取得的 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 例外狀況為 SmsConnectionException 和 SmsQueryException。 這些可以與 SmsException一起攔截。
另請參閱
物件概觀Configuration Manager 延遲屬性如何使用 Managed 程式代碼呼叫 Configuration Manager 物件類別方法如何使用 Managed 程式代碼連接到 Configuration Manager 提供者如何建立使用 Managed 程式代碼 Configuration Manager 物件 如何使用 Managed 程式代碼修改 Configuration Manager 物件如何使用 Managed 程式代碼執行異步 Configuration Manager 查詢如何執行同步Configuration Manager 使用 Managed 程式代碼查詢如何使用 Managed 程式代碼讀取 Configuration Manager 物件