DscResources icon indicating copy to clipboard operation
DscResources copied to clipboard

Get-DscResource throws an error when there is an Inheritance in class based DSC Resources

Open SimeonGerginov opened this issue 5 years ago • 3 comments

Description: There seems to be a problem when having base classes for DSC Resources you want to reuse. The problem occurs when having the 'Using module' clause, if you define the base class in the same file, no error is thrown. However, this way you cannot reuse base classes for multiple DSC Resources.

Full Example:

BaseDSC class is defined in a separate module 'Base'.

class BaseDSC {
    [DscProperty(Key)]
    [string] $Server
}

With 'Using module' I import it in the Resource module:

Using module Base

[DscResource()]
class DscResource : BaseDSC {
    [DscProperty()]
    [string] $Name
}

When you import the module and call Get-DscResource:

Import-Module -Name <Name of Module>
Get-DscResource

The following error is thrown:

Exception calling "ImportClassResourcesFromModule" with "3" argument(s): "Class feature already defined: Server
At line:9, char:23
Buffer:
[key]string Server;^
[r
"
At C:\windows\system32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\PSDesiredStateConfiguration.psm1:2573 char:5
+ $resourcesFound = [Microsoft.PowerShell.DesiredStateConfiguration ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : CimException

The problem only occurs when calling Get-DscResource in the same process as Import-Module. If we Import the module in one process and call Get-DscResource in another, the error does not occur.

SimeonGerginov avatar Mar 19 '19 13:03 SimeonGerginov

I'm afraid that's not a bug from a DSC Resource of the Resources Kit, but a PowerShell one.

As you are using Windows PowerShell, your best bet is probably to open a support request with Microsoft, but not sure it'd go very far. If you can reproduce on PS Core, you might want to file it on the PowerShell/PowerShell github repository.

Btw, I love the work you guys do there 🤓

I'll leave that open for now so that the community can see it, learn, and maybe find tips & work around.

gaelcolas avatar Mar 26 '19 02:03 gaelcolas

Hi @SimeonGerginov, I struggle to reproduce your error. Could you please let me know what's your $PSversionTable.PSVersion Also do a get-module -listAvailable PSDesiredStateConfiguration (could be my environment that has been altered). Could you provide a repository with the modules and tests you run?

To attempt to reproduce I created two modules:

├───ClassExtends
│       ClassExtends.psd1
│       ClassExtends.psm1
│
├───ClassRsrcWithSubType2
│       ClassRsrcWithSubType2.psd1
│       ClassRsrcWithSubType2.psm1

The ClassRsrcWithSubType2 is just a Class based resource that works fine. The Second is this, as per your issue:

Using module ClassRsrcWithSubType2

[DscResource()]
class ClassExtends : ClassRsrcWithSubType2 {
    [DscProperty()]
    [string] $stuff

}

When I do: ipmo ClassRsrcWithSubType2 then Get-DsCResource I have no (related) issues as you mention, and it does find my ClassExtends resource.

I do see why it could fail (if it duplicates the class entry in the cache), but it does not seem to (for me).

gaelcolas avatar Aug 26 '19 06:08 gaelcolas

  1. PowerShell version
PS C:\windows\system32> $PSversionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      17763  592
  1. Desired State Configuration Module
PS C:\windows\system32> Get-Module -ListAvailable PSDesiredStateConfiguration


    Directory: C:\windows\system32\WindowsPowerShell\v1.0\Modules


ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Manifest   1.1        PSDesiredStateConfiguration         {Set-DscLocalConfigurationManager, Start-DscConfiguration, Test-DscConfiguration, Publish-DscConfiguration...}

Well based on your example I get the impression that ClassRsrcWithSubType2 is a Class based DSC Resource but in my case BaseDSC is not a DSC Resource. It is a PowerShell class only that is then extended by the DSC Resources. So that may be the reason why the error is not reproduced.

P.S. If the bug is still not reproduced, I will provide an example module with more details.

SimeonGerginov avatar Sep 02 '19 11:09 SimeonGerginov