Welcome › Forums › DSC (Desired State Configuration) › Error converting hastable to ciminstance
- This topic has 4 replies, 2 voices, and was last updated 3 years, 3 months ago by
Inactive.
-
AuthorPosts
-
-
August 18, 2016 at 11:12 pm #51016
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 : ArchrDvAOCmgmtMOF
[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 #51036
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 #51038
-
August 19, 2016 at 2:03 am #51040
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 #51077
-
-
AuthorPosts
- The topic ‘Error converting hastable to ciminstance’ is closed to new replies.