Hashtable as parameter for Custom DSC Resource

This topic contains 8 replies, has 2 voices, and was last updated by Profile photo of Timor Kaleman Timor Kaleman 11 months, 3 weeks ago.

  • Author
    Posts
  • #32440
    Profile photo of Timor Kaleman
    Timor Kaleman
    Participant

    Hello, I am trying to build a Custom DSC Resource using the New-xDscResource Designer.
    I am getting a problem when I want to use the Hashtable type.
    Instead of using the [System.Collections.Hashtable] object it creates the parameter with the [Microsoft.Management.Infrastructure.CimInstance[]] object and when I want to deliver a Hashtable parameter to it in the DSCConfiguration.ps1 file, The actual parameter comes as [Microsoft.Management.Infrastructure.CimInstance[]] instead of a proper Hashtable that I can work with as I want to.

  • #32441
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    That's normal. MOF files don't have any concept of a hashtable, so when you compile a configuration, it gets turned into an array of key-value pairs. If you want to use a hashtable in your code, you can write a simple function to take that array of CimInstance objects and turn it back into a hashtable for your code to use:

    function Convert-CimInstancesToHashtable([Microsoft.Management.Infrastructure.CimInstance[]] $Pairs)
    {
        $hash = @{}
        foreach ($pair in $Pairs)
        {
            $hash[$pair.Key] = $pair.Value
        }
    
        return $hash
    }
    
  • #32443
    Profile photo of Timor Kaleman
    Timor Kaleman
    Participant

    Excellent job, works like a charm, big thanks.

  • #32444
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    No problem! 🙂

  • #32760
    Profile photo of Timor Kaleman
    Timor Kaleman
    Participant

    OK, now I have encountered in another problem.
    I would like to pass to a parameter like in the example above, a hashtable which contains another hashtable/s in it, for example:

    a = @{
    b = @{
    "b1" = "test1";
    };
    c = @{
    "b2" = "test2"
    }
    } ;

    Currently it throws an exception when I try to set a parameter with that hashtable. Does that mean that DSC currently supports setting only 1 level of a hashtable in a parameter?

  • #32766
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Hmm.... I don't see any reason that CIM couldn't represent that, but the DSC module probably isn't trying to do a recursive conversion on hashtables.

    It seems like a bit of a code smell, at any rate. DSC resources are supposed to be fairly small / discrete, handling a single thing. If you need to pass in a parameter that complicated, then maybe it's an indication that what you should be doing is making multiple calls to the same DSC resource rather than trying to make one call and passing in a huge amount of information.

  • #32778
    Profile photo of Timor Kaleman
    Timor Kaleman
    Participant

    Maybe for most cases you are right and I can call multiple times for the same resource, but for my scenario I think I must deliver a multiple level hashtable as parameter.
    I succeeded to convert the hashtable into a 1 level and then transmit it by doing it.
    a = @{
    "b.b1" : "test"
    "b.b2" : "test2"
    "c.c1" : "test3"
    }
    But I don't think that how things should work, do you know where can I place a request to handle multiple level hashtables as parameters for Custom Resouces in DSC?

  • #32779
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    You'd probably have to file a bug or suggestions on Connect (though I think they're transitioning over to UserVoice now).

    In the meantime, perhaps you should consider using something like JSON. Then your MOF property just has to be a string, and PowerShell can easily convert hashtables to/from JSON with the ConvertTo-Json and ConvertFrom-Json cmdlets.

  • #33163
    Profile photo of Timor Kaleman
    Timor Kaleman
    Participant

    Thanks, did like you said with JSON as string

You must be logged in to reply to this topic.