Configuration Manager でコレクション変数を作成する方法

Configuration Manager コレクションのコレクション変数を作成するには、サーバー WMI クラスSMS_CollectionVariableインスタンスをサーバー WMI クラスの CollectionVariables プロパティに追加しますSMS_CollectionSettings。

コレクション変数を作成するには

  1. SMS プロバイダーへの接続をセットアップします。 詳細については、「 SMS プロバイダーの基礎」を参照してください。

  2. SMS_CollectionSettings のインスタンスを取得します。

  3. 追加する変数ごとに、埋め込みオブジェクト SMS_CollectionVariable のインスタンスを CollectionVariables 配列プロパティに追加します。

  4. SMS_CollectionSettings クラス インスタンスに変更をコミットします。

次の例のメソッドでは、コレクション変数を作成し、指定された識別子で識別されるコレクションに追加します。 コレクションの SMS_CollectionSettings オブジェクトが存在しない場合は作成されます。

サンプル コードの呼び出しの詳細については、「Configuration Manager コード スニペットの呼び出し」を参照してください。

Sub CreateCollectionVariable( connection, name, value, mask, collectionId, precedence)

    Dim collectionSettings
    Dim collectionVariables
    Dim collectionVariable
    Dim Settings

    ' See if the settings collection already exists. if it does not, create it.
    Set settings = connection.ExecQuery _
      ("Select * From SMS_CollectionSettings Where CollectionID = '" & collectionID & "'")

    If settings.Count = 0 Then
        Wscript.Echo "Creating collection settings object"
        Set collectionSettings = connection.Get("SMS_CollectionSettings").SpawnInstance_
        collectionSettings.CollectionID = collectionId
        collectionSettings.Put_
    End If

    ' Get the collection settings object.
    Set collectionSettings = connection.Get("SMS_CollectionSettings.CollectionID='" & collectionId &"'" )

    ' Get the collection variables.
    collectionVariables=collectionSettings.CollectionVariables

    ' Create and populate a new collection variable.
    Set collectionVariable = connection.Get("SMS_CollectionVariable").SpawnInstance_
    collectionVariable.Name = name
    collectionVariable.Value = value
    collectionVariable.IsMasked = mask

    ' Add the new collection variable.
    ReDim Preserve collectionVariables (UBound (collectionVariables)+1)
    Set collectionVariables(UBound(collectionVariables)) = collectionVariable

    collectionSettings.CollectionVariables=collectionVariables

    collectionSettings.Put_

 End Sub
public void CreateCollectionVariable(
    WqlConnectionManager connection,
    string name,
    string value,
    bool mask,
    string collectionId,
    int precedence)
{
    try
    {
        IResultObject collectionSettings = null;

        // Get the collection settings. Create it if necessary.

         IResultObject collectionSettingsQuery = connection.QueryProcessor.ExecuteQuery(
                    "Select * from SMS_CollectionSettings where CollectionID='" + collectionId + "'");

         foreach (IResultObject setting in collectionSettingsQuery)
         {
             collectionSettings = setting;
         }

        if ( collectionSettings == null)
         {
             collectionSettings = connection.CreateInstance("SMS_CollectionSettings");
             collectionSettings["CollectionID"].StringValue = collectionId;
             collectionSettings.Put();
             collectionSettings.Get();
         }

        // Create the collection variable.
        List<IResultObject> collectionVariables = collectionSettings.GetArrayItems("CollectionVariables");
        IResultObject collectionVariable = connection.CreateEmbeddedObjectInstance("SMS_CollectionVariable");
        collectionVariable["Name"].StringValue = name;
        collectionVariable["Value"].StringValue = value;
        collectionVariable["IsMasked"].BooleanValue = mask;

        // Add the collection variable to the collection settings.
        collectionVariables.Add(collectionVariable);
        collectionSettings.SetArrayItems("CollectionVariables", collectionVariables);

        // Set the collection variable precedence.
        collectionSettings["CollectionVariablePrecedence"].IntegerValue = precedence;

        collectionSettings.Put();
    }
    catch (SmsException e)
    {
        Console.WriteLine("Failed to create collection variable: " + e.Message);
        throw;
   }
}

サンプル メソッドには、次のパラメータがあります:

パラメーター 説明
Connection - 管理対象: WqlConnectionManager
- VBScript: SWbemServices
SMS プロバイダーへの有効な接続。
Name - 管理対象: String
- VBScript: String
作成する変数の名前。
Value - 管理対象: String
- VBScript: String
変数の値
Mask - 管理対象: Boolean
- VBScript: Boolean
値が Configuration Manager コンソールに表示されるかどうかを指定します。

true - 変数値は表示されません。

false - 変数値が表示されます。
CollectionID - 管理対象: String
- VBScript: String
変数が追加されるコレクション。
Precedence - 管理対象: Integer
- VBScript: Integer
配列内の他の変数に対する変数の優先順位。

コードのコンパイル

C# の例では、次のようなコンパイル要件があります。

名前空間

System

System.Collections.Generic

Microsoft.ConfigurationManagement.ManagementProvider

Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine

Assembly

Microsoft.ConfigurationManagement.ManagementProvider

adminui.wqlqueryengine

堅牢なプログラミング

エラー処理の詳細については、「Configuration Manager のエラーについて」を参照してください。

.NET Framework のセキュリティ

Configuration Manager アプリケーションのセキュリティ保護の詳細については、「Configuration Manager ロールベースの管理」を参照してください。

関連項目

オブジェクトの概要マネージ コードを使用して Configuration Manager の SMS プロバイダーに接続する方法WMI を使用して Configuration Manager の SMS プロバイダーに接続する方法Configuration Manager でコンピューター変数を作成する方法マネージド コードを使用した Configuration Manager オブジェクトWMI を使用して Configuration Manager オブジェクトを作成する方法 OS展開について コンピューターの管理