Getting Started with PowerShell Desired State Configuration (DSC)

As I’ve mentioned in other posts, Desired State Configuration (DSC) is a powerful technology with a lot of potential. However, due to how new it is and how rapidly it’s evolving, it can be difficult to get started and figure out how to accomplish your specific goals. My intention here is not to give an exhaustive look at the ins and outs of DSC (I’m not qualified to do that), but rather to give you the tools to get started and be successful with it .

Step 1: Get a Baseline

After years of cobbling together information, then having to go back later and relearn how to do it the right way, I’ve learned the value of getting the framework right from the start. If you have a strong framework in mind for how something is built and how it’s intended to be used, then building useful things on top of it is much easier. I had the same experience when learning DSC. There is a lot of information on the internet about it, but much of it is “old” and out of date. Also, everyone has their own opinions for how it should be used. Eventually I came across two videos on Microsoft Virtual Academy that put everything into perspective. They are taught by “Jeffrey Snover, Microsoft Distinguished Engineer and inventor of PowerShell,  along with Windows PowerShell MVP Jason Helmick”. This is a fantastic starting point for diving into DSC and I can’t recommend them enough.

Getting Started with PowerShell Desired State Configuration (DSC) – Microsoft Virtual Academy

Advanced PowerShell Desired State Configuration (DSC) and Custom Resources – Microsoft Virtual Academy

Step 2: Working With New Resources

Now that you know all about DSC and how it can be used, it’s time to put that knowledge to work. You eagerly download the module you want to use (let’s say xSQLServer, for example) and are ready to have machines install SQL for you. The first inclination is probably to google it to see how it’s used, which will lead you to github or PowerShell Gallery. Those are great for getting information about the package and its change tracking, but not much use for actually implementing the module. So here you are with a brand new toy and no manual.

Examples

The first thing to do with a new module is always check for an examples folder. Your module was probably installed in C:\Program Files\WindowsPowerShell\Modules.

Opening the examples folder within that module will reveal a list of scripts made by the creating team for various scenarios they see the module being used in. Your mileage may vary depending on the module you’re using and who made it, but generally those produced by Microsoft have useful information. This is where I obtained the example file that my last post, “Installing SQL Server Using Desired State Configuration“, was based on. Again, how much explanation is included within each script is completely up to the discretion of the creator. That’s why it’s important to first watch the videos linked above. Then, whether there is proper documentation or not you can make sense of it yourself.

(Upon further inspection, the examples are sometimes also available on the github site.)

Interrogating Resources

Even when you have an example file that closely matches your needs it’s likely that you will still want to customize it. Many times, the module you are working with will have a resource you need but not an example of it listed in the file. Or you simply want to know what all you can do with the module. As usual, it’s PowerShell to the rescue.

Using PowerShell we can easily find which resources are available to us in a module using Get-DscResource.

After finding a resource that interests us, we can dig further down into its specifics. In order to get more than cursory information, it s necessary to expand the properties field.

From this information we can tell which fields are available to us, what their data types are, and which ones are required.

It should also be noted that you can discover these from right within ISE as well. This is the improvised method that I used before discovering the PowerShell cmdlets above. If you type anything within the resource block that it doesn’t recognize, intellisense will automatically suggest the proper fields to use by hovering over the angry red line.

And, if all else fails, in the end resources are just PowerShell scripts. You can go to their folder and open them like any other file (ie C:\Program Files\WindowsPowerShell\Modules\xSQLServer\DSCResources\MSFT_xSQLServerLogin).

Step 3: Be Brave

Armed with all of this knowledge there is but one thing to do, be brave. Start putting some configurations together, make mistakes, then use the lessons learned to make better configurations. This is an exciting technology in which things are rapidly moving and changing. In fact, within days of my last post (and while writing this one) I discovered that xSQLServer had been retired in favor of SqlServerDsc, and I’d had no idea.

So get at it, make your own creations, keep your eyes open daily for changes, and let me know if you have any questions. I look forward to learning with you.

Leave a Reply

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