How to Run a Configuration Manager Query

Applies To: System Center Configuration Manager 2007, System Center Configuration Manager 2007 R2, System Center Configuration Manager 2007 R3, System Center Configuration Manager 2007 SP1, System Center Configuration Manager 2007 SP2

In Microsoft System Center Configuration Manager 2007, you run a SMS_Query based query by getting the query instance and then by running WQL query in the SMS_Query object Expression property.

After you have the WQL query, you can run the query either synchronously or asynchronously. The following example is synchronous. For information about running the query asynchronously, see How to Perform an Asynchronous Configuration Manager Query by Using Managed Code and How to Perform an Asynchronous Configuration Manager Query by Using WMI. In these examples, change the select * from collection string to the Expression property value.

To run a query

  1. Set up a connection to the SMS Provider. For more information, see About the SMS Provider in Configuration Manager.

  2. Get the SMS_Query object for the query you want to run.

  3. Run the query identified by the SMS_Query object Expression property.

Example

The following example method synchronously runs the query identified by the queryId parameter.

For information about calling the sample code, see Calling Configuration Manager Code Snippets.

Sub RunQuery(connection, queryId)
    Dim query
    Dim queryResults
    Dim queryResult 
    
    
    ' Get query.
    Set query=connection.Get("SMS_Query.QueryID='" & queryId  & "'" )

    If err.number<>0 Then
        WScript.echo "Couldn't get Queries"
        Exit Sub
    End If

    ' Run query.
    WScript.echo query.Name
    WScript.echo "----------------------------------"

    Set queryResults=connection.ExecQuery(query.Expression)
    For Each queryResult In queryResults
        wscript.echo "     " & queryResult.Name
    Next
    If queryResults.Count=0 Then
        WScript.echo "      no query results"
    End If
End Sub
public void RunQuery(WqlConnectionManager connection, string queryId)
{
    try
    {
        // Get the query.
        IResultObject query = connection.GetInstance(@"SMS_Query.QueryID='" + queryId + "'");

        Console.WriteLine(query["Name"].StringValue);
        Console.WriteLine("----------------------------------");

        // Get the query results.
        IResultObject queryResults = connection.QueryProcessor.ExecuteQuery(query["Expression"].StringValue);

        bool resultsFound = false;
        foreach (IResultObject queryResult in queryResults)
        {
            resultsFound = true;
            Console.WriteLine(queryResult["Name"].StringValue);
        }
        if (resultsFound == false)
        {
            Console.WriteLine("     No query results");
        }
    }
    catch (SmsException ex)
    {
        Console.WriteLine("Failed to run query: " + ex.Message);
        throw;
    }
}

The example method has the following parameters:

Parameter

Type

Description

connection

  • Managed: WqlConnectionManager

  • VBScript: SWbemServices

A valid connection to the SMS Provider.

queryID

  • Managed: String

  • VBScript: String

A query identifier. For more information see the SMS_Query class QueryID property.

Compiling the Code

The C# example has the following compilation requirements:

Namespaces

System

System.Collections.Generic

System.Text

Microsoft.ConfigurationManagement.ManagementProvider

Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine

Assembly

microsoft.configurationmanagement.managementprovider

adminui.wqlqueryengine

Robust Programming

For more information about error handling, see About Configuration Manager Errors.

Security

For more information about securing Configuration Manager applications, see About Securing Configuration Manager Applications.

See Also

Concepts

About Configuration Manager Queries
How to Create a Configuration Manager Query
How to Perform an Asynchronous Configuration Manager Query by Using Managed Code
How to Perform an Asynchronous Configuration Manager Query by Using WMI