Probably one of the most interesting 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 always configured the way you need. This can be achieved by creating and deploying special 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 very simple configuration script which will include the following baseline items.
- A custom directory on disk
- Altering the behavior of a windows service
Start with by 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. Given that this is just a PowerShell script, it’s 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 given. Some resource providers, the part responsible for managing the execution of an instruction, have been included by default however, Microsoft releases new providers on a regular basis. In addition, many new providers have been built by the PowerShell community. And if your desired instruction is missing, you could decide to build your own.
Adding new instructions:
The first instruction can be used to managed the state of the services running on the target system. The code below will make sure that the Spooler serves remains Disabled, however more options are available and can be found 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 a intermediate file format called .MOF which needs to be generated first. Fortunately this is a very simple process.
Configuration script -> Generate .MOF files -> Deploy .MOF files
First make sure to load the 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 that the .MOF files have been created, it’s time to deploy the configuration by using the following command.
This will result in a Job allowing you keep track of the deployment process using the familiar PowerShell Job cmdlets. After the jobs have been completed 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 and many new Configuration Resources are in Beta.
Windows PowerShell Desired State Configuration Resources – http://technet.microsoft.com/en-us/library/dn282125.aspx