如何创建Configuration Manager查询

在 Configuration Manager 中,通过创建 SMS_Query实例来创建基于 的SMS_Query查询。 类SMS_QueryExpression对象定义 WQL 查询。 如果要将查询结果限制为特定集合,请在 属性中 LimitToCollectionID 指定集合标识符。

注意

创建查询时,它将显示在Configuration Manager控制台的“查询”下。

创建查询

  1. 设置与 SMS 提供程序的连接。 有关详细信息,请参阅 SMS 提供程序基础知识

  2. 创建 SMS_Query 实例。

  3. 填充 SMS_Query 属性。

  4. SMS_Query提交 。

  5. 如果需要,请检索查询对象并获取查询标识符。

示例

以下示例方法创建一个 SMS_Query 类查询,用于查询所有系统。 方法返回查询标识符,该标识符可用作如何运行Configuration Manager查询中的示例的输入。

有关调用示例代码的信息,请参阅调用Configuration Manager代码片段

Function CreateQuery(connection)  
   On Error Resume Next  

   Dim query  
   Dim path  

   ' Create a query object.  
    Set query = connection.Get("SMS_Query").SpawnInstance_()  

    If Err.Number<>0 Then  
        Wscript.Echo "Couldn't create query object"  
        CreateQuery = Null  
        Exit Function  
    End If  

    ' Populate the object.  
    query.Comments = "A query for all systems"  
    query.Expression = "select Name, " + _  
    "SMSAssignedSites, " +              _  
    "IPAddresses, " +                   _  
    "IPSubnets, " +                     _  
    "OperatingSystemNameandVersion, " + _  
    "ResourceDomainORWorkgroup, " +     _  
    "LastLogonUserDomain, " +           _  
    "LastLogonUserName, " +             _  
    "SMSUniqueIdentifier, " +           _  
    "ResourceId, " +                    _  
    "ResourceType, " +                  _  
    "NetbiosName " +                    _  
    "from sms_r_system"  
    query.LimitToCollectionID = nothing  
    query.Name = "Query All Systems"  
    query.TargetClassName = "SMS_R_System"  

    ' Commit the object  
    path = query.Put_  

    If Err.Number<>0 Then  
        Wscript.Echo "Couldn't commit the query"  
        CreateQuery = Null  
        Exit Function  
    End If  

    WScript.Echo "Query created"  

    ' Get the object back to get the query identifier.  
    Set query = connection.Get(path)  
    CreateQuery = query.QueryID  

End Function  

public string CreateQuery(WqlConnectionManager connection)  
{  
    try  
    {  
        // Create an SMS_Query object.  
        IResultObject query = connection.CreateInstance("SMS_Query");  

        // Populate the object.  
        query["Comments"].StringValue = "A query for all systems";  
        query["Expression"].StringValue =   
            "select Name, " +  
            "SMSAssignedSites, " +  
            "IPAddresses, " +  
            "IPSubnets, " +  
            "OperatingSystemNameandVersion, " +  
            "ResourceDomainORWorkgroup, " +  
            "LastLogonUserDomain, " +  
            "LastLogonUserName, " +  
            "SMSUniqueIdentifier, " +  
            "ResourceId, " +  
            "ResourceType, " +  
            "NetbiosName " +  
            "from sms_r_system";  
        query["LimitToCollectionID"].StringValue = null;  
        query["Name"].StringValue = "Query All Systems";  
        query["TargetClassName"].StringValue = "SMS_R_System";  

        // Commit the query.  
        query.Put();  

        // Get the query - allows access to the queryID.  
        query.Get();  

        // Return the query identifier.  
        return query["QueryID"].StringValue;  
    }  
    catch (SmsException e)  
    {  
        Console.WriteLine("Failed to run the query: " + e.Message);  
        throw;  
    }  
}  

示例方法具有以下参数:

参数 类型 说明
connection -管理: WqlConnectionManager
- VBScript: SWbemServices
- 与 SMS 提供程序的有效连接。

编译代码

C# 示例具有以下编译要求:

命名空间

System

System.Collections.Generic

System.Text

Microsoft。ConfigurationManagement.ManagementProvider

Microsoft。ConfigurationManagement.ManagementProvider.WqlQueryEngine

Assembly

microsoft.configurationmanagement.managementprovider

adminui.wqlqueryengine

可靠编程

有关错误处理的详细信息,请参阅关于Configuration Manager错误

.NET Framework 安全性

有关保护Configuration Manager应用程序的详细信息,请参阅Configuration Manager基于角色的管理

另请参阅

关于Configuration Manager查询
如何运行Configuration Manager查询