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:
Scripting this by hand we get something like this:
- Configured values should be separate from tasks
- Tasks should be idempotent
- Logging - we need to know what happened
- Handling for dependencies
New-item -path $Directory
write-host "Creating folder $Directory..."
write-host "Directory, $Directory, already exists.
No work was done..."
In DSC, we have a few steps:
- Create a configuration
- Create a MOF file
The configuration looks like this:
DestinationPath = $folderName
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.
"c:\MyOtherApp" -MachineName localhost
Lastly, we can deploy the folder with the following command:
Start-DscConfiguration -Path .\AppDirectories
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.
Here's a good introduction:
A list of built in resources for DSC:
Labels: DSC, POSH, Release Management