-1

I've written two of my own DSC resources. When I call Get-DscConfiguration on a computer running PowerShell 4 that uses one of these resources (PowerShell 5 is not affected, I get these errors:

Get-DscConfiguration : Unable to cast object of type 'System.Management.Automation.SwitchParameter' to type 'System.IConvertible'

and

Get-DscConfiguration : Unable to cast object of type 'System.String' to type 'Microsoft.Management.Infrastructure.Native.InstanceHandle'.

What's going on? What is DSC trying to convert and how to I get this error to go away?

Aaron Jensen
  • 25,861
  • 15
  • 82
  • 91

1 Answers1

0

This happens when a value's type in the hashtable returned by a resource's Get-TargetResource function doesn't match or can't be implicitly converted to, the type defined in a resource's MOF.

My first DSC resource's MOF declares this property:

[Write]
boolean CaseSensitive;

My Get-TargetResource function declares this a Switch parameter:

[Switch]
# The INI file being modified is case-sensitive.
$CaseSensitive,

It was getting returned like this:

return @{
            # snip 
            CaseSensitive = $CaseSensitive;
        }

The solution was to cast the switch myself:

return @{
            # snip 
            CaseSensitive = [bool]$CaseSensitive;
        }

My second resource's MOF declares a PSCredential property:

[Write,
 EmbeddedInstance("MSFT_Credential")] 
string TaskCredential;

My Get-TargetResource function declares this a PsCredential object:

[Management.Automation.PSCredential]
# The principal the task should run as. Use `Principal` parameter to run as a built-in security principal. Required if `Interactive` or `NoPassword` switches are used.
$TaskCredential,

By default, this property was getting returned as an empty string:

$resource = @{
                 TaskCredential = '';
             }

The solution was to set TaskCredential's default value to $null:

$resource = @{
                 TaskCredential = $null;
             }
Aaron Jensen
  • 25,861
  • 15
  • 82
  • 91