Syntax Basics
Overview of DSC Configurations¶
Desired State Configuration (DSC) in PowerShell uses configurations to define the desired state of a system. A configuration script describes resources (e.g., files, services, features) and their desired properties. When applied, DSC ensures the system matches the defined state.
Configuration Structure¶
A DSC configuration is defined using the Configuration keyword, followed by a name and a Node block specifying target systems. Resources are added using their resource names and property sets.
Configuration <ConfigurationName> {
Node <ComputerName> {
<ResourceName> <ResourceInstanceName> {
<Property1> = <Value1>
<Property2> = <Value2>
...
}
}
}
<ConfigurationName>: The name of the configuration (e.g.,EnsureIIS).<ComputerName>: The target node (e.g.,localhostor a remote computer).<ResourceName>: The DSC resource (e.g.,WindowsFeature,xRemoteFile).<ResourceInstanceName>: A unique name for the resource instance.
Example: Ensuring IIS is Installed¶
This example ensures the IIS feature is installed on a local machine:
Configuration EnsureIIS {
Node localhost {
WindowsFeature IIS {
Name = "Web-Server"
Ensure = "Present"
}
}
}
Explanation:
- The WindowsFeature resource ensures the "Web-Server" feature is installed.
- Ensure = "Present" specifies the desired state.
Applying the Configuration¶
After defining the configuration, compile it into a MOF file and apply it:
# Generate the MOF file
EnsureIIS -OutputPath .\Output
# Apply the configuration
Start-DscConfiguration -ConfigurationName EnsureIIS -OutputPath .\Output -Wait -Verbose
-OutputPath: Specifies where the MOF file is saved.-Wait: Blocks until the configuration completes.-Verbose: Provides detailed output during application.
Key Takeaways¶
- DSC configurations use the
Configurationblock to define resources and target nodes. - Resources are added with their specific property sets (e.g.,
WindowsFeature). - The
Start-DscConfigurationcmdlet applies the configuration to the target system. - MOF files are generated during compilation and applied to enforce the desired state.