Error converting hastable to ciminstance

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of Gaurav Mishra Gaurav Mishra 1 month, 1 week ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #51016
    Profile photo of Gaurav Mishra
    Gaurav Mishra
    Participant

    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
    )

    #51036
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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.

    #51038
    Profile photo of Gaurav Mishra
    Gaurav Mishra
    Participant

    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.

    #51040
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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
    
    }
    
    #51077
    Profile photo of Gaurav Mishra
    Gaurav Mishra
    Participant

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

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.