次の方法で共有


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 クエリを実行する方法