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 ?
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:
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
}