Condividi tramite


Come creare una query Configuration Manager

In Configuration Manager si crea una SMS_Queryquery basata su creando un'istanza di SMS_Query. L'oggetto SMS_Query classe Expression definisce una query WQL. Se si desidera limitare i risultati della query a una raccolta specifica, specificare l'identificatore della raccolta nella LimitToCollectionID proprietà .

Nota

Quando si crea una query, viene visualizzata nella console Configuration Manager in Query.

Per creare una query

  1. Configurare una connessione al provider SMS. Per altre informazioni, vedere Nozioni fondamentali sul provider SMS.

  2. Creare un'istanza di SMS_Query.

  3. Popolare le SMS_Query proprietà.

  4. Eseguire il commit di SMS_Query.

  5. Se necessario, recuperare l'oggetto query e ottenere l'identificatore di query.

Esempio

Il metodo di esempio seguente crea una SMS_Query query di classe che esegue query per tutti i sistemi. Il metodo restituisce l'identificatore di query, che può essere usato come input per l'esempio in Come eseguire una query Configuration Manager.

Per informazioni sulla chiamata del codice di esempio, vedere Chiamata di frammenti di codice 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;  
    }  
}  

Il metodo di esempio include i parametri seguenti:

Parametro Tipo Descrizione
connection -Gestito: WqlConnectionManager
- VBScript: SWbemServices
- Connessione valida al provider SMS.

Compilazione del codice

L'esempio C# presenta i requisiti di compilazione seguenti:

Namespaces

Sistema

System.collections.generic

System.Text

Microsoft. ConfigurationManagement.ManagementProvider

Microsoft. ConfigurationManagement.ManagementProvider.WqlQueryEngine

Assembly

microsoft.configurationmanagement.managementprovider

adminui.wqlqueryengine

Programmazione efficiente

Per altre informazioni sulla gestione degli errori, vedere Informazioni sugli errori di Configuration Manager.

Sicurezza di .NET Framework

Per altre informazioni sulla protezione delle applicazioni Configuration Manager, vedere Configuration Manager'amministrazione basata sui ruoli.

Vedere anche

Informazioni sulle query Configuration Manager
Come eseguire una query Configuration Manager