MOF complex types implemented?

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Dave Wyatt Dave Wyatt 2 years, 3 months ago.

  • Author
    Posts
  • #18034
    Profile photo of Martin Nielsen
    Martin Nielsen
    Participant

    During my research I came across this site http://msdn.microsoft.com/en-us/library/dn529073(v=vs.85).aspx explaining how to author a MOF schema file for an OData Web Service.

    The site does the following:

    class PswsTest_Process
    {
        [Key] SInt32 Id;
        ..
        [Required, EmbeddedInstance("PswsTest_ProcessModule")] String Modules[];
        ..
    };
    
    [ComplexType]
    class PswsTest_ProcessModule
    {
        String ModuleName;
        String FileName;
    };
    

    I tried getting an adaption of this to work in my DSC configuration, but I'm having trouble getting it to acknowledge my DSCResource, so I'm wondering if anyone knows whether this functionality is implemented in DSC1.0 ?

  • #18040
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Yep, take a look at xWebsite / cWebsite (https://github.com/PowerShellOrg/DSC/tree/master/Resources/cWebAdministration/DSCResources/PSHOrg_cWebsite) for an example of this.

    A configuration that uses cWebsite and the embedded PSHOrg_cWebBindingInformation instances looks like this:

    configuration AConfig
    {
        Import-DscResource -ModuleName cWebAdministration
    
        cWebsite AWebsite
        {
            Name = 'MyWebsite'
            BindingInfo = @(
                PSHOrg_cWebBindingInformation
                {
                    Port = 80
                    Protocol = 'http'
                    IPAddress = '*'
                }
    
                PSHOrg_cWebBindingInformation
                {
                    Port = 443
                    Protocol = 'https'
                    IPAddress = '*'
                    CertificateThumbprint = 'BlahBlahBlah'
                }
    
                # etc
            )
        }
    }
    

    Edit: This is for embedded instances... I'm not sure where "Complex Type" comes into play.

  • #18041
    Profile photo of Martin Nielsen
    Martin Nielsen
    Participant

    Oh this is fantastic! It's exactly what I was looking for (I'm honestly not too sure what difference it being a complex type or not makes).

    This is precisely what I've been struggling with in my other thread, trying to pass hashtables back and forth.

  • #18046
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Hashtables and PSCredentials are special cases; DSC has some behavior specifically for those types, to make it easier on people. For an example of Hashtables in action, check out the xRemoteFile resource (part of the DSC resource kit, in the xPSDesiredStateConfiguration module. The Schema.mof looke like this:

    	[Write, EmbeddedInstance("MSFT_KeyValuePair"), Description("Headers of the web request.")] String Headers[];
    

    The resource's psm1 file looks like this:

    function Set-TargetResource
    {
        [CmdletBinding()]
        param
        (
            [Microsoft.Management.Infrastructure.CimInstance[]]
            $Headers
        )
    )
    

    Pretty much what you'd expect to see for any other embedded instance type of property. However, in the configuration file, you can do this:

    xRemoteFile remoteFile
    {
        Headers = @{
            Key1 = 'Value 1'
            Key2 = 'Value 2'
        }
    }
    

    And DSC will take care of converting that hashtable literal in the configuration script into the array of MSFT_KeyValuePair objects that the MOF document and Resource will use. Same thing goes for PSCredential objects; configuration scripts can use those directly, and DSC converts them for you.

    As for making use of these KeyValuePair arrays in a resource, you might want to convert them back into a hashtable. xRemoteFile has code for that as well:

    $headersHashtable = $null
    
    if ($Headers -ne $null)
    {
        $headersHashtable = Convert-KeyValuePairArrayToHashtable -array $Headers
    }
    
    #... 
    
    # Converts CimInstance array of type KeyValuePair to hashtable
    function Convert-KeyValuePairArrayToHashtable
    {
        param (
            [parameter(Mandatory = $true)]
            [Microsoft.Management.Infrastructure.CimInstance[]]
            $array
        )
    
        $hashtable = @{}
        foreach($item in $array)
        {
            $hashtable += @{$item.Key = $item.Value}
        }
    
        return $hashtable
    }
    
    

You must be logged in to reply to this topic.