Desired State Configuration (DSC)
Desired State Configuration (DSC) is a management platform in PowerShell that enables you to manage your IT and development infrastructure. It’s a declarative, model-driven solution that allows you to configure, deploy, and manage your infrastructure consistently across your organization.
What is DSC?
DSC allows you to define the desired state of your systems (like servers, applications, and environments) using configuration documents. These documents are written in a declarative syntax, meaning you specify *what* you want your system to look like, not *how* to achieve it. DSC then ensures that your systems are configured to match these desired states.
Key Concepts
- Declarative Configuration: You describe the desired state of your resources.
- Idempotency: DSC configurations can be applied multiple times without unintended side effects. Each application brings the system closer to the desired state.
- Resources: The building blocks of DSC, which represent specific configuration items like files, registry keys, services, or installed applications.
- Configurations: PowerShell scripts that define the desired state of one or more resources.
- Local Configuration Manager (LCM): The engine on each node that interprets and applies configurations.
Resources
DSC resources are the fundamental units of configuration. Each resource type has a defined set of properties that describe a specific aspect of a system. Examples of built-in DSC resources include:
File
: Manages files and directories.Registry
: Manages registry keys and values.Service
: Manages Windows services.Package
: Installs and manages software packages.Script
: Allows you to run custom PowerShell scripts as part of a configuration.
You can also create custom DSC resources to manage any aspect of your infrastructure that isn't covered by the built-in resources.
Example: Managing a File
This example ensures a specific file exists with specific content.
Configuration SetFileContent
{
Node 'localhost'
{
File HelloWorldFile
{
DestinationPath = "C:\Temp\HelloWorld.txt"
Contents = "Hello, DSC World!"
Ensure = "Present"
}
}
}
Configurations
A DSC configuration is a PowerShell script that contains one or more node blocks. Each node block defines the desired state for a specific target node.
Configurations are compiled into MOF (Managed Object Format) files, which are then applied to the target nodes.
Example: Simple Configuration
Configuration MyWebAppConfig
{
Param (
[string[]]$ComputerName = 'localhost'
)
Node $ComputerName
{
WindowsFeature IIS
{
Ensure = 'Present'
Name = 'Web-Server'
}
Service 'W3SVC'
{
Name = 'W3SVC'
State = 'Running'
DependsOn = '[WindowsFeature]IIS' # Ensure IIS is installed first
}
}
}
Local Configuration Manager (LCM)
The Local Configuration Manager (LCM) is the core engine on each target node responsible for executing DSC configurations. It handles:
- Receiving and processing configurations.
- Calling the appropriate DSC resources to bring the node into the desired state.
- Reporting on the status of configurations.
You can configure the LCM's behavior, such as its refresh mode (pull or push) and how often it checks for configuration changes.
Getting Started
To start using DSC:
- Ensure you have PowerShell 5.0 or later installed.
- Define your desired state using DSC configuration scripts.
- Compile your configuration into a MOF file.
- Apply the MOF file to your target nodes using either a push mode (deploying directly from your management machine) or a pull server (where nodes check in to retrieve their configurations).
Advanced Topics
- Custom Resources: Develop your own resources for unique management needs.
- DSC Pull Server: Set up a central server for nodes to fetch their configurations.
- DSC Classes: Use object-oriented approaches for defining resources.
- DSC Conditions: Implement logic and dependencies within configurations.
- DSC Best Practices: Learn how to write robust and maintainable configurations.
Explore the links in the sidebar for more detailed documentation on each of these topics.