Automation Ninja's Dojo

PowerShell – Creating DSC module – Part 2

Hey ,

Without any further delays we will continue our excercise to write yp DSC module containing multiple resources. As this is multi-part post you might want to jump back to first post to read up on preparations and considerations.

 

Objectives for this post:

In this particular post we will focus on creating DSC resource properties and modifying out root module manifest.  Source code in ps1 format for what we will be doing today is available as usual 🙂 on GitHub  ( https://github.com/RafPe/cWebAdmin/blob/master/src/createDscResources.ps1 ) so if you would like to help or have feedback or suggestions leave a comment or just fork the script and request a pull 😀

If you are new to this series of post I’m aiming to get fully operational webapp Pool and website admin DSC module (at the moment of writing this post it will support only IIS 8.5 ). If you would say “Hey! But there is already one from MS available even on GitHub! So why reinvent the wheel ?” then my answer would be ‘The ones available do not perform scope of configurations I want to do – therefore I’m creating one to fullfuill my technical needs and on the other hand I get a nice tech excercise! ‘

 

Investigate your target resource properties:

Let’s go ahead and see what’s available for us. As usual there are multipe ways to achieve your goal 😀 its all about selecting the optimal one. As in majority cases you could go ahead and investigate GUI to see which ones are available for you. Something in lines of :

2015-08-09_18h10_51

 

Hmmmm … but really – how much time will it take you to complete list of all available properties ? So lets go ahead and do it a bit differently.

That gives you more less output similar to the following one :

2015-08-09_18h15_19

 

So is that all ? 🙂 Of course it isnt – we are missing quite a lot. The remaining properties are hidden in child elements collections. We get to them by calling :

And that gives you the following output :

2015-08-09_18h17_43

 

And you access the properties of each collection by calling

and our output will look similar to the following one :

2015-08-09_18h22_12

 

 

Fine 🙂 we have our resources and more less we know their types (keep in mind that some of int types here – within GUI are enums )

 

Get me the resource!

Ok – so you are hungry for coding 😀 I totally udnerstand – So let’s discuss cmdlet that will be playing main role here for us : which is New-xDscResourceProperty.

This cmdlet has couple of properties which we need to discuss. Lets take a look on example of usage :

As you can see we specify couple of important switches :

  1. Name : I think that this will be self explanatory 🙂 What you need to keep in mind that single DSC resource CANNOT HAVE 2 properties with the same name (but you can imagine why 🙂 )
  2. Type This defines what type is the property we will be working with
  3. Attribute :
    [ Key ]     : 
    on a property signals that the property uniquely identifies the resource
    [ Write ] : means we can assign value in our configuration
    [ Read ]  : indicates that property cannot be changed , neither we can assign value to it
  4. Description : Allows you to describe property
  5. Values and ValuesMap : restricts possible property values to specified in ValuesMap

 

It’s coding time 😀

 

Since now you know how to go about this – lets go ahead and create our resources properties and finally our resource. The decision how to tackle all properties and creating each one I leave up to you. At the moment of writing this post in series my script have had defined all of them (however this could have changed 🙂 )

Since I want to create multiple resource properties I define them as following :

 

Next to ease assigning them into a new resource I create an array

 

And once all of that is done we can go ahead and finally create our new resource! In my case this is resource to manage WebApp pool default settings.

As you can see here I’m prefixing the resource name with RafPe_ and I’m doing this because a lot of other people could come up with same name. Altough FriendlyName I’m choosing to be cWebAppPoolDefaults as thats the way I want resource to show up. Next we define our root module name which in my case is cWebAdminFollowed by Path which in this case points to default modules folder. And of course the most important – I specify our resource properties which we have just defined a moment ago.

Once you are happy with your module just hit enter! And after couple of seconds your new module should be ready and good to go!

You should now have your module already available for import .

2015-08-09_19h02_15

 

Modify root DSC module manifest:

Whats now left – we need to modify module manifest. Easiest (and a lot of ppl do it ) is to copy one of already existsing ones and just changed it a bit. What is extremly important is to generate own new GUID 

Module manifest for root module we are working with looks following :

 

And how do you generate a Guid ? simple … use .Net methods to quickly do that :

 

Summary:

Well thats it for today 🙂 We have narrowed our resources and evaluated their types. We discussed how to create resource property and what parameters we can use fot that instance . And lastly we initially created our module and modified its manifest. So I think we are on really good track to start write of our resource methods in next post in this series.

 

Stay tuned for more! Catch up with all changes/Comment / Pull ->  on GitHub (https://github.com/RafPe/cWebAdmin) and till then !

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: