Configuration Manager クエリを作成する方法

Configuration Manager では、SMS_Query のインスタンスを作成してSMS_Queryベースのクエリを作成します。 SMS_Query クラス Expression オブジェクトは、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 クエリを実行する方法