2

カスタム DSC リソースを作成していて、特定のプロパティをリソースのキーの一部にしたいのですが、同時にオプションにすることもできます。

  • ユーザーが構成で指定する場合は、同じ値で 2 つのインスタンスを作成できないように、キーの一部にする必要があります。

  • ユーザーが設定しない場合は、それもキーの一部であるが null 値を持つかのように動作する必要があります。これにより、ユーザーは他のすべてのキーが同じであるが、このオプションのパラメーターを設定せずに複数のリソースをインスタンス化することはできません。

基本的に私が欲しいのは次のとおりです。

schema.mof ファイル

[ClassVersion("1.0.0.0"), FriendlyName("cMyResource")]
class Mobiltec_cMyResource : OMI_BaseResource
{
    [Key, Description("Name")] string Name;
    [Key, Description("Key1")] string Key1;
    [Key, Description("This is a key only if it is specified")] string OptionalKey2;
    [Write, Description("Ensures"), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] string Ensure;
};

私が望む実装の簡単な表現:

function Set-TargetResource
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory)][string]$Name,
        [Parameter(Mandatory)][string]$Key1,

        # Note that this is not mandatory
        [string]$OptionalKey2,

        [ValidateSet("Present","Absent")][string]$Ensure = "Present"
    )
...
}

そして使用法:

有効:

cMyResource Res1
{
    Name = 'name'
    Key1 = 'key1'
    OptionalKey2 = 'key2'
    OtherParameter = 'param'
}

cMyResource Res2
{
    Name = 'name'
    Key1 = 'key1'
    OptionalKey2 = 'otherKey2'
    OtherParameter = 'param'
}

また有効:

cMyResource Res1
{
    Name = 'name'
    Key1 = 'key1'
    OtherParameter = 'param'
}

cMyResource Res2
{
    Name = 'name'
    Key1 = 'otherKey1'
    OtherParameter = 'param'
}

cMyResource Res3
{
    Name = 'name'
    Key1 = 'otherKey1'
    OptionalKey2 = 'key2'
    OtherParameter = 'param'
}

ただし、プロパティをキーとして宣言すると、リソースを指定せずに構成で使用しようとすると、次のエラーが発生します。

cMyModule\cMyResource : クラス 'cMyResource' では、プロパティ 'OptionalKey2' にタイプ 'String' の値を指定する必要があります。:167 文字:9 + cMyResource Res1 + ~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [書き込みエラー]、ParentContainsErrorRecordException + FullyQualifiedErrorId : MissingValueForMandatoryProperty、 cMyModule\cMyResource

4

1 に答える 1