Author Posts

August 18, 2016 at 11:12 pm

I have a dsc resource to which I need to pass hashtable, I used xResourceDesigner to create resource and hash table param are converted to ciminstance, now when I am trying to pass hashtable after converting it to array i am getting below error.

Cannot convert the "System.Collections.Hashtable" value of type "System.Collections.Hashtable" to type "Microsoft.Management.Infrastructure.CimInstance[]".
+ CategoryInfo : InvalidArgument: (:) [], CimException
+ FullyQualifiedErrorId : ConvertToFinalInvalidCastException
+ PSComputerName : ArchrDvAOCmgmt

MOF

[ClassVersion("1.0.0.0"), FriendlyName("CLM_CreateArcherUser")]
class CLM_CreateArcherUser : OMI_BaseResource
{
[Key] String KeyProperty;
[Required] String ApiBaseAddress;
[Required] String SqlConnection;
[Required, EmbeddedInstance("MSFT_KeyValuePair")] String LogInDetails[];
[Required, EmbeddedInstance("MSFT_KeyValuePair")] String UserDetails[];
[Required, EmbeddedInstance("MSFT_KeyValuePair")] String SecParamDetails[];
};

Configuration

#Create user detail hashtable for each node
$UserDetails = @{
FirstName= $UserToConfigure.FirstName
LastName= $UserToConfigure.LastName
EMailId= $UserToConfigure.EMailId
UserName= $UserToConfigure.UserName
GroupName= $UserToConfigure.GroupName
Password= $plainPassword
SecurityParameterName= $UserToConfigure.SecurityParameterName
}

[System.Collections.ArrayList] $UserDetailsArrayList = New-Object System.Collections.ArrayList
foreach($key in $UserDetails.Keys )
{
[String] $key = $key
[String] $value = $UserDetails[$key]
$UserDetailsArrayList.Add(@{$key = $value}) | Out-Null
}

CLM_CreateArcherUser $UserToConfigure.UserName
{
KeyProperty = $UserToConfigure.UserName
ApiBaseAddress = $Node.ArcherURL
SqlConnection = $Node.SqlConnectionString
LogInDetails = $LogInDetailsArrayList.ToArray()
UserDetails = $UserDetailsArrayList.ToArray()
SecParamDetails = $SecParamDetailsArrayList.ToArray()
PsDscRunAsCredential = $credentials

}

I am converting $LogInDetailsArrayList and $SecParamDetailsArrayList as well before passing.

module :-

function Set-TargetResource
{
[CmdletBinding()]
param
(
[parameter(Mandatory = $true)]
[System.String]
$KeyProperty,

[parameter(Mandatory = $true)]
[System.String]
$ApiBaseAddress,

[parameter(Mandatory = $true)]
[System.String]
$SqlConnection,

[parameter(Mandatory = $true)]
[Microsoft.Management.Infrastructure.CimInstance[]]
$LogInDetails,

[parameter(Mandatory = $true)]
[Microsoft.Management.Infrastructure.CimInstance[]]
$UserDetails,

[parameter(Mandatory = $true)]
[Microsoft.Management.Infrastructure.CimInstance[]]
$SecParamDetails
)

August 19, 2016 at 12:36 am

You're passing arrays of hashtables to your DSC resource, which isn't needed. Just pass in a single hashtable, and DSC will take care of converting it into an array of key-value pairs when you compile the MOF. So you'd have UserDetails = $UserDetails , etc.

August 19, 2016 at 1:55 am

I am not passing array of hash table i am passing single hastable, when I open mof I see all the values converted to key pair. But still I am getting this error.

August 19, 2016 at 2:03 am

Yes, you are. It's right there in your code. 🙂


# Here, you're creating an arraylist containing multiple hashtable objects (each with a single key)

 [System.Collections.ArrayList] $UserDetailsArrayList = New-Object System.Collections.ArrayList
foreach($key in $UserDetails.Keys )
{
[String] $key = $key
[String] $value = $UserDetails[$key]
$UserDetailsArrayList.Add(@{$key = $value}) | Out-Null
}

# Here, you're passing the .ToArray() method from your ArrayList to the DSC resource

CLM_CreateArcherUser $UserToConfigure.UserName
{
KeyProperty = $UserToConfigure.UserName
ApiBaseAddress = $Node.ArcherURL
SqlConnection = $Node.SqlConnectionString
LogInDetails = $LogInDetailsArrayList.ToArray()
UserDetails = $UserDetailsArrayList.ToArray()
SecParamDetails = $SecParamDetailsArrayList.ToArray()
PsDscRunAsCredential = $credentials

}

August 19, 2016 at 6:21 am

Thank you u are right. The error I got was because of code i wrote to convert ciminsatance to hashtabe. I fixed it.