PowerShell – Desired State Configuration

PowerShell - Desired State Configuration

Probably one of the most exciting features included within PowerShell 4 is the option to create configuration scripts, also known as DSC. Yes, this is something new – unless you are familiar with Chef and Puppet. And no, this isn’t the same as executing a large provisioning script.

PowerShell DSC – Before we begin

DSC stands for desired state configuration and helps you to ensure that your servers are configured the way you need. You can do this by creating and deploying configuration scripts written using PowerShell syntax.

After deploying the script to the instance, the local configuration manager will take care of keeping the system configuration in sync with the desired configuration within a set Correction Interval, and therefore eliminating the need to write and configure the sync logic yourself.

The main benefit of this is that you will be able to ensure that the systems are using the baseline configuration, which is trivial in my case, provisioning development and test environments, avoiding some ugly and time-consuming debugging scenarios.

Note: Before covering some basic samples, it’s important to know that this will require the Windows Management Framework 4 and the .NET framework 4.5 to be present on the target system.

Building a script

Now let’s start building a straightforward configuration script, which will include the following baseline items.

  • A custom directory on disk
  • Altering the behavior of a windows service

Start with creating a new PowerShell script file, including the template, as shown below.

Note: MyName represents the name of the configuration and NameOfTheSystem refers to the name of the system. This is just a PowerShell script, and therefoe very simple to replace hard-coded values with variables.

At this stage, we are ready to include some instructions. One important thing to know is that the local configuration manager will need to be familiar with the instructions. Some resource providers, the part responsible for managing the execution of an instruction, have been included by default. Microsoft releases new providers regularly. And if your desired instruction is missing, you could decide to build your own.

Adding new instructions:

The first instruction should is used to manage the state of the services running on the target system. The code below will make sure that the Spooler serves Disabled; however, more options are available on TechNet – DSC Service Resource.

The second sample will force the existence of a Directory on disc. More configuration options are available on TechNet – DSC File Resource.

At this stage, we have completed the sample configuration script and ready to apply the configuration on our target server; however, the Windows Management Framework only accepts an intermediate file format called.MOF, which you need to generate first. Fortunately, this is a straightforward process.

Configuration script -> Generate .MOF files -> Deploy .MOF files

First, make sure to load the configuration into memory by executing the script. And run the CoreDSC command.

Note: Please note that the files will be generated within the same directory by default.

Now it’s time to deploy the configuration by using the following command.

The deployment will result in a Job run allowing you to keep track of the deployment process using PowerShell Job cmdlets. After the job completion the jobs successfully, the configuration instructions should have been applied and corrected if altered.

Please keep in mind that this is just a very basic example. Complex push and pull deployment scenarios are supported. Also, many new Configuration Resources are in Beta.

Additional resources:

Windows PowerShell Desired State Configuration Resources – http://technet.microsoft.com/en-us/library/dn282125.aspx

Post Navigation