Error converting hastable to ciminstance

Welcome Forums DSC (Desired State Configuration) Error converting hastable to ciminstance

Viewing 4 reply threads
  • Author
    Posts
    • #51016
      Inactive
      Topics: 3
      Replies: 3
      Points: 0
      Rank: Member

      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
      Member
      Topics: 9
      Replies: 2322
      Points: 0
      Rank: Member

      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
      Inactive
      Topics: 3
      Replies: 3
      Points: 0
      Rank: Member

      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
      Member
      Topics: 9
      Replies: 2322
      Points: 0
      Rank: Member

      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
      Inactive
      Topics: 3
      Replies: 3
      Points: 0
      Rank: Member

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

Viewing 4 reply threads
  • The topic ‘Error converting hastable to ciminstance’ is closed to new replies.