Understanding Desired State Configuration (Advanced)
With the release of Windows PowerShell v4.0, one new feature is introduced: Desired State Configuration. In fact, it is available as a feature in Windows Server 2012 R2 Preview OS. Windows PowerShell Desired State Configuration is a set of extensions and providers that enable to declare, repeatedly deploy, and configure data center resources. DSC enables us to define the configuration of the target nodes (computers or devices) and prevent configuration inconsistencies.
Getting ready
We are living in a world where device adoption is quicker than ever. Hence, we must ensure that all the devices are in a desired state. DSC allows us to manage all the devices using Windows PowerShell.
How to do it...
The DSC flow can be categorized into two different models: Push and Pull. The DSC process could be divided in to three phases as follows:
The Authoring phase: In this phase, we need to declare the DSC configuration. The outcome of this phase would be of one or more Management Object Format (MOF) files, a format that is consumable by DSC. MOF files can be created in multiple ways; the simpler one is that of using Windows PowerShell ISE. Using PowerShell v4, we can add declarative syntax extensions and IntelliSense for making it easier to create an MOF file. It handles schema validations as well.
The Staging phase: In this phase, actions are carried out based on the model that we are using either Push or Pull. In case of adopting the Pull Model, DSC data and custom providers are kept on the Pull server. A Pull server is an IIS web server with a well-defined OData interface. The target system contacts the Pull Server by passing a URI and a unique identifier to retrieve its configuration and verifies whether all the custom providers are available. If not, they are downloaded to the target system. If we're using the Push Model, DSC data is being pushed to the target system.
One catch though is that you need to make sure your custom provider exists on the target system. You need to place them at
"%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\PSProviders"
.The "Make it so" phase: The final phase is to apply the configuration to "make it so". The DSC data is either pulled or pushed to the Local Configuration Store and contains the current, previous, and the desired state configuration (DSC). The configuration then gets parsed and the relevant provider (WMI) implements the change and "makes it so".
How to define the Configuration block
The following is a sample configuration
block to declare configuration for one or more nodes under the name MyWebConf
:
configuration MyWebConf { # Parameters are optional param ($MachineName, $WebsitePath) # A Configuration block can have one or more Node blocks node $MachineName { # Next, specify one or more resource provider blocks # WindowsFeature is one of the providers you can use in a Node block # This example ensures the Web Server (IIS) role is installed WindowsFeature IIS { Name = "Web-Server" # Use the Name property from Get-WindowsFeature Ensure= "Present" # To uninstall the role, set Ensure to "Absent" } # You can use the File provider to create files and folders # "File" is the name of the resource provider to use # "WebDirectory" is the name you want to use to refer to this instance File WebDirectory { SourcePath = $WebsiteFilePath DestinationPath = "C:\inetpub\wwwroot" Requires = "[WindowsFeature] IIS" # Use Requires for dependencies Ensure = "Present" # You can also set Ensure to "Absent" } } }
How it works...
The DSC feature consists of multiple subcomponents. The information is as follows:
Local Configuration Manager (LCM): This is a part of the DSC system that implements configuration data on the target nodes.
Windows PowerShell language extensions: DSC extends the Windows PowerShell language to support importing the MOF schema files that are converted to Windows PowerShell keywords. The keywords are used to describe the desired configuration of resources in the data center. The following are the two keywords:
Configuration: This defines the desired configuration
Node: This defines the desired configuration for one or more nodes
File share pull provider: The file share pull provider is an auxiliary module that is used by LCM to retrieve the configuration of MOF files and providers from a file share or a local folder.