2015-01-06
I have often used powershell to script out deployment steps. This ensures that each deployment accomplishes the same tasks and nothing is missed from environment to environment. What I did not know about though, was DSC (Desired State Configuration) which I think was introduced in powershell 4.0. DSC is a declarative model that allows the author to specify the end result and let powershell do the work to get things in that state.
In this post, I will show an example of creating a folder in scripting and using DSC. There are a few things that we want out of a deployment process:
- Configured values should be separate from tasks
- Tasks should be idempotent
- Logging - we need to know what happened
- Handling for dependencies
Scripting this by hand we get something like this:
$Directory = "c:\MyApp"
if(!(Test-path $Directory))
{
New-item -path $Directory
-ItemType Directory
write-host "Creating folder $Directory..."
}
else
{
write-host "Directory, $Directory, already exists.
No work was done..."
}
In DSC, we have a few steps:
- Create a configuration
- Create a MOF file
- Deploy
The configuration looks like this:
Configuration AppDirectories
{
param($folderName,
$MachineName)
Node $MachineName
{
File $folderName
{
DestinationPath = $folderName
Ensure =
"Present"
Type =
Directory
}
}
}
Configuration is a keyword to state that this is a DSC configuration. AppDirectories will be the name of the configuration. The configuration can take parameters, so in this case we will supply the $foldername. Node is the name of the target machine. File is a keyword for a resource - this specifies that we are working on a file or directory and certain inputs are expected. There are a number of other In-Built resources which I put a link to at the bottom. Ensure makes sure that the file exists.
Once we load the configuration, we can call it and input our configurations for a particular deployment. This will create a folder in the current directory and generate a MOF file in that.
AppDirectories -folderName
"c:\MyOtherApp" -MachineName localhost
Lastly, we can deploy the folder with the following command:
Start-DscConfiguration -Path .\AppDirectories
-Wait -Verbose
This gives the following output:

What's interesting is that these configurations can be made very generic for reusability. They come with logging, they are already idempotent and you can specify dependencies on other configurations. You can copy files, force the overwrite, copy whole directories recursively. The in-built resources can manage environment variables, windows services, windows configurations and so on. You can also create your custom resources.
One last note, you may need to run powershell as an admin for permissions.
References:
Here's a good introduction:
http://blogs.technet.com/b/privatecloud/archive/2013/08/30/introducing-powershell-desired-state-configuration-dsc.aspx
A list of built in resources for DSC:
http://technet.microsoft.com/en-us/library/dn249921.aspx
Mark Wojciechowicz
Labels: DSC, POSH, Release Management