Author Posts

March 21, 2018 at 5:53 pm

Hi all,

I currently work on a Class-based DSC resource and face some problems (seems you cannot have a Class-based resources and a composite one in the same module).

As I look at differents resources in the DSC Resource kit, I haven't found any Class-based resource; only Mof-based ones.

Is there a reason to avoid Class-based ? Are there advantage(s) to use Classes to build DSC resources ?

March 21, 2018 at 10:31 pm

Hi,

If you look in C:\Windows\System32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration, you should see two folders: DSCResources and DSCClassResources. This looks like MS's attempt to combine the two types, however PSDesiredStateConfiguration resource is now on PowershellGallery and that no longer has the DSCClassResources folder and the resource in it has now been changes to MOF. Draw what conclusions you will.

I have read on Stack Overflow that it isn't possible, however, I have also read through the issues on Github where someone is trying to contribute a class resource to an existing MOF resource. The repo owner doesn't say it's not possible. Quite the opposite. I'll try and find it when I'm at my PC in the morning, however I think it was the SqlServerDsc resource.

I have tried it and had to give up. I've written about it here: http://blog.thingsgeeky.co.uk/?p=10454

What I don't say (need to update it) is that it all works until I actually run the configuration at which point it errors, complaining about a missing class. In the end, I had to break the class resources out into its own module.

I got close and I'm sure there is a way, it just needs input from someone that knows what they're doing!

HTH

W.

March 22, 2018 at 8:08 am

Thank you Tex for your detailed answer. You are right, PSDesiredStateConfiguration is a mix of 2 kinds of resources and works, as I understand, with the 'NestedModule' paramter in the main psd1. It's also mentioned by Ryan in https://powershell.org/forums/topic/two-clsss-dsc-resources-in-one-module/

My question is now: what are the advantages of building Class-based resource (especially if you are not a .Net developer) ?

March 22, 2018 at 8:39 am

Hi Julien,

Apologies, I thought after posting, I hadn't actually answered the question you asked.

For me personally, I find the class based resources easier and I am no developer, the problem is that most of the public resources are MOF based and I can't work out how to get a mix of class and MOF resources to work in the same "module". However, it might not be a requirement to wrap them into one module.

The actual .Net part of the resource is / can be fairly small and the rest can be written in traditional PS.

I guess it's a case of personal preference, although class-based resources mean the MOF file is written out for you, so one less step. There may be some technical differences, but from the field, I haven't found any negative ones yet and this includes extensive use in Azure.

When I have to write my own, I will always see if I can get away with writing class-based before MOF.

HTH,

W.

March 22, 2018 at 9:07 am

Thank-you Tex ! It's true that Class-based have a clearer structure, even for non developer.

In addition, based on your findings, we where able to mix Mof-based, composites and Class-based resources in the same module. This is how we did:

File structure:

myModule
├── myModule.psd1
├── DSCResources
│   ├── myMofBasedResource
│   |   ├── myMofBasedResource.psd1
│   |   ├── myMofBasedResource.psm1
│   |   └── myMofBasedResource.schema.mof
│   ├── myCompositeResource
│   |   ├── myCompositeResource.psd1
│   |   └── myCompositeResource.psm1
└── DSCClassResources
    └── myClassBasedResource
        ├── myClassBasedResource.psd1
        └── myClassBasedResource.psm1

In the myModule.psd1

...
NestedModules = 'DSCClassResources\myClassBasedResource\myClassBasedResource.psd1
...
DscResourcesToExport = 'myClassBasedResource'

Note that the myModule.psd1 doesn't mention resources located in the DSCResources.

We found out that Class-Based resources are case-sensitive, at least in the class definition and it's PSD1.

Thanks again 🙂

March 22, 2018 at 9:15 am

Thanks Julien,

I'll have a look over my notes and compare. I'll see if I can find where I went wrong.

Good to know that it is possible (at least on-prem, a whole different story in Azure!!!)

T, W.