Windows Powershell Desired State Configuration

With the release of Powershell 4.0, we can now leverage Powershell DSC across Server 2012 and Server 2008 R2. This is a pretty good foot print for supporting any modern infrastructure. In my opinion, DSC is one of the most powerful features to be added to Powershell since WinRM. It provides a great foundation for generating very solid infrastructure automation for Windows Servers.

There’s a number of great resources for learning DSC, see the links below. But I’ll summarize the key features.

  • Ability to define using declarative scripting the server configuration. This is a huge improvement over traditional scripts, because they’re easier to implement and maintain over time. This functionality has existed in the Linux world for some time using tools like CFEngine, Puppet, and Chef. But it was clunky to implement for Windows.
  • Ability to separate data from configuration declarations.
  • Ability to implement custom functionality. Out of the box DSC comes with a number of basic resources that cover most situations, but implementing custom functionality is not very difficult.

Example

# Config Values
$ApplicationSource = "\\ATL0WSRCP001\Applications\MyApp"

Configuration MyWebConfig
{
   Node "ATL0WIISP001"
   {
      # This ensures the Web Server (IIS) role is installed
      WindowsFeature IIS7-Install
      {
          Ensure = "Present"
          Name = "Web-Server"  
      }

      # File is a built-in resource you can use to manage files and directories
      # This example ensures files from the source directory are present in the destination directory
      File Application-Files
      {
         Ensure = "Present"
         Type = "Directory"
         Recurse = $true
         SourcePath = $ApplicationSource # This is a path that has the source files
         DestinationPath = "E:\Applications\MyApp" # The path where we want to ensure the web files are present
         Requires = "[WindowsFeature]IIS7-Install"  # This ensures that IIS7-Install completes successfully before this block runs
      }
   }
}

Resources

My New Stories

March 2016 Web Hosting Deals
Powershell AD Group Management
Troubleshooting 403