Custom DSC resource does not implement the key property...

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Adam Weigert Adam Weigert 1 year, 7 months ago.

  • Author
    Posts
  • #25630
    Profile photo of Adam Weigert
    Adam Weigert
    Participant

    I am working on a custom DSC resource in WMF 4. I've tested the individual *-TargetResource cmdlets and I know they work, however I am having an issue getting it to apply. I receive an error when I run Start-DscConfiguration against the generated mof:

    The command Get-TargetResource of the PS resource DSC_cNetAdapterAdvancedSettings does not implement the key property Value mentioned in the corresponding MOF schema file ... as a mandatory parameter. All key parameters mentioned in the schema file must be implemented by the command Get-TargetResource as mandatory function parameters.

    Below is the an example of the resource I'm trying to use. I have found if I change the Value property to write instead of requires I get further but I really want this property to be required and from all the docs I've read this is a valid config, though I haven't seen examples with a required array... hoping someone with a little more experience can decipher why DSC is failing.

    [cNetAdapterAdvancedProperty.schema.mof]

    [ClassVersion("1.0.0.0"), FriendlyName("cNetAdapterAdvancedSettings")] 
    class DSC_cNetAdapterAdvancedSettings : OMI_BaseResource
    {
        [Key] string InterfaceAlias;
        [Key] string Name;
        [required] string Value[];
        [write] boolean NoRestart = false;
    };

    [cNetAdapterAdvancedProperty.psm1]

    function Get-TargetResource
    {
        [OutputType([System.Collections.Hashtable])]
        param
        (
            [Parameter(Mandatory)]
            [ValidateNotNullOrEmpty()]
            [string] $InterfaceAlias,
    
            [Parameter(Mandatory)]
            [ValidateNotNullOrEmpty()]
            [string] $Name,
    
            [Parameter(Mandatory)]
            [ValidateNotNullOrEmpty()]
            [string[]] $Value
        )
    
        $Property = Get-NetAdapterAdvancedProperty -Name $InterfaceAlias | Where-Object RegistryKeyword -eq $Name
    
        if ($Property) {
            $Value = $Property.RegistryValue
        } else {
            $Value = @()
        }
    
        @{
            InterfaceAlias = $InterfaceAlias
            Name           = $Name
            Value          = $Value
        }
    }
    
    function Set-TargetResource
    {
        param
        (
            [Parameter(Mandatory)]
            [ValidateNotNullOrEmpty()]
            [string] $InterfaceAlias,
    
            [Parameter(Mandatory)]
            [ValidateNotNullOrEmpty()]
            [string] $Name,
    
            [Parameter(Mandatory)]
            [ValidateNotNullOrEmpty()]
            [string[]] $Value,
    
            [switch] $NoRestart = $false
        )
    
        Set-NetAdapterAdvancedProperty -Name $InterfaceAlias -RegistryKeyword $Name -RegistryValue $Value -NoRestart:$NoRestart
    }
    
    function Test-TargetResource
    {
    	[OutputType([System.Boolean])]
        param
        (
            [Parameter(Mandatory)]
            [ValidateNotNullOrEmpty()]
            [string] $InterfaceAlias,
    
            [Parameter(Mandatory)]
            [ValidateNotNullOrEmpty()]
            [string] $Name,
    
            [Parameter(Mandatory)]
            [ValidateNotNullOrEmpty()]
            [string[]] $Value,
    
            [switch] $NoRestart = $false
        )
    
        $Property = Get-NetAdapterAdvancedProperty -Name $InterfaceAlias | Where-Object RegistryKeyword -eq $Name
    
        if ($Property) {
            if (Compare-Object -ReferenceObject $Property.RegistryValue -DifferenceObject $Value) {
                return $false
            } else {
                return $true
            }
        } 
        
        return $false
    }
    
    Export-ModuleMember -Function *-TargetResource

    [Configuration.ps1]

    Configuration Config
    {
      Node localhost {
        cNetAdapterAdvancedProperty JumboPacket {
          InterfaceAlias = 'Ethernet'
          Name = '*JumboPacket'
          Value = '9014'
        }
      }
    }

    [localhost.mof]

    instance of DSC_cNetAdapterAdvancedSettings as $DSC_cNetAdapterAdvancedSettings1ref
    {
    ResourceID = "[cNetAdapterAdvancedSettings]JumboPacket";
     Value = {
        "9014"
    };
     Name = "*JumboPacket";
     ModuleName = "cNetworking";
     InterfaceAlias = "Ethernet";
     ModuleVersion = "1.0";
    };
  • #25638
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Looks correct to me. If you've been changing the contents of your module on the fly, you may need to either bump up the version number in the PSD1 file, kill the wmiprvse process, or enable DebugMode in the LCM (if you're running a patched version of WMF 4 which has Debug Mode.) The LCM is pretty aggressive about caching resources, by default, and it may be running an older version of your code.

  • #25645
    Profile photo of Adam Weigert
    Adam Weigert
    Participant

    Killing the wmi process let it see the new code. Thanks man.

    gps wmi* |? {$_.Modules.ModuleName -like '*DSC*'} | Stop-Process -Force

You must be logged in to reply to this topic.