Writting custom DSC resource

During the European PowerShell summit, the hackaton was focused on 4 subjects, but only one direction:  Build a custom DSC resource for the community.

It was very intresting, and like after each IT Event, you come home with many many ideas… Your wife still doesn’t like it but… Anyway, if you weren’t at the summit:

  • First, it’s a mistake, you should really come next year 😉
  • Second, i will try to share the magic building your own DSC resource.

 

For this exemple, i will take a really easy challenge. So to make this possible, you need, PowerShell v5 (or newer) and an internet connexion.

This post will only talk about using MOF files resources, with the v5 preview, you can also use classes to build resource, which means that you don’t even need mof files now. But classes are kinda buggy for now, so let’s focus on the old way.

There is a module to help you building resources, his name is xDSCResourceDesigner, so let’s download it.

 

Ok, we have found the module, now let’s install it !

You will be prompted by a windows, because you we don’t have the -force switch with the cmdlet!

dsc2

Now, as we have our cmdlet available, the resource will provide the possibility to mount or unmout a VHD(x) file on a system. It’s not a very hard task, but it helps me really understand the meaning of the generated resource script. So let’s build the resource script, and folder directories.

First, here is the code used to generate the DSC resource

Let me explain what this cmdlet does.

The cmdlet will create a directories tree like this

dsc4

 

And as you see, a psm1 file and a schema.mof file has appeared.

the psm1 file is the most important, it store the 3 functions used by DSC when you call a resource:

  • Get-TargetResource – This function will always return a hastable. It’ll gather information about the task we wanted to know, and return them in a hashtable.
  • Set-TargetResource – This function will apply what we want to do. For our example it’ll mount or unmount the VHDx file.
  • Test-TargetResource – This function will test the system so see if the set-targetresource should be applied or not. It’ll always return a boolean.

Ok, so now, let’s edit our psm1 file to add our stuff about mounting a VHD(x) !

We have now our module ready to import ?

Ahhehhhm i don’t think so. We need to build a manifest file in order to enable the import-module cMountVHD possible !

A manifest file is a *.psd1 file named like the module, and stored in module root, in our case it’s “C:\Program Files\WindowsPowerShell\Modules\cMountVHD”.

The following lines are enough to have a correct manifest file.

Remember that if you have multiple resource in your module, you will need a new psd1 in each resource! And a root module should exists in the module manifest.

Now if you check at your module list you should see your cMountVHD appears

sqc5

 

So, now in our DSC script we can import this resource !

Let’s see if this is working smoothly !

First execution:

dsc6

 

Second Exectuion, nothing happens, as we want.

dsc7

 

 

Third Execution, if we put the Ensure value to Absent, let’s see if our test is OK, and the VHDx correctly unmounted !

dsc8Seems it’s working 🙂

Another tips when building your resource, if you want to shate it with community, which is far better, you should prefix the name by c, it means Community released !

I hope this post will bring you to the DSC darkside.