Table of Contents

PowerShell DSC (Desired State Configuration)

(DevOps deployment navbar)

Return to GitOps, DevOps deployment, Kubernetes-Docker, Linux configuration (Unix shell initialization - bash profile) Ansible-Terraform, Package manager, Package management, Packages, Configuration, Configuration automation, Deployment automation, Continuous deployment, Delivery, Continuous delivery, Integration, Continuous integration, CI-CD, CI/CD, Version control, GitHub, GitHub Actions, GitHub CLI, AWS Cloud Shell, AWS Tools for PowerShell, Azure Cloud Shell, Azure PowerShell, Google Cloud Shell, Oracle Cloud Shell (Oracle Cloud Infrastructure Cloud Shell), IBM Cloud Shell, Linode Cloud Shell, DigitalOcean Cloud Shell, GitHub Codespaces IDE, AWS Cloud9 IDE, Databricks Cloud Shell, JetBrains, Azure DevOps, JetBrains TeamCity, Management, Configuration management

PowerShell DSC (Desired State Configuration) is a management platform in PowerShell that enables administrators to manage the configuration of systems declaratively. It allows you to define the desired state of your environment using configuration scripts, and then ensures that the target systems match this state. DSC helps automate the deployment and management of infrastructure, ensuring consistency and reducing manual effort.

  1. Key Concepts of PowerShell DSC

1. **Configuration**:

  - A configuration is a declarative script written in [[PowerShell]] that specifies the desired state of a system. Configurations describe how you want the target systems to be set up.

2. **Resources**:

  - Resources are the building blocks of [[DSC]] configurations. Each resource defines how to configure a specific aspect of the system, such as files, services, packages, and user accounts. There are built-in resources, and you can also create custom resources.

3. **MOF File**:

  - The Managed Object Format (MOF) file is a compiled version of the configuration script. It is generated by running the configuration script and is used by the Local Configuration Manager (LCM) to apply the configuration to the target systems.

4. **Local Configuration Manager (LCM)**:

  - The LCM is the [[DSC]] engine that runs on each target node. It processes the MOF file, applies the desired configuration, and ensures that the system remains in the desired state.

  1. Creating and Applying a DSC Configuration
  1. Step 1: Write a Configuration Script

Here’s an example of a basic DSC configuration script that ensures the IIS web server is installed and running:

```powershell Configuration WebServerConfig {

   Import-DscResource -ModuleName PSDesiredStateConfiguration
   Node "WebServerNode" {
       WindowsFeature IIS {
           Name = "Web-Server"
           Ensure = "Present"
       }
       Service "W3SVC" {
           Name = "W3SVC"
           Ensure = "Running"
           DependsOn = "[WindowsFeature]IIS"
       }
   }
}

  1. Generate the MOF file

WebServerConfig ```

  1. Step 2: Generate the MOF File

Run the configuration script to generate the MOF file:

```powershell WebServerConfig -OutputPath “C:\DSC\Configurations” ```

  1. Step 3: Apply the Configuration

Use the `Start-DscConfiguration` cmdlet to apply the configuration to the target node:

```powershell Start-DscConfiguration -Path “C:\DSC\Configurations” -Wait -Verbose ```

  1. DSC Resources
  1. Built-in Resources

- **File**: Manages files and directories. - **Service**: Manages Windows services. - **Registry**: Manages registry keys and values. - **WindowsFeature**: Manages Windows features and roles.

Example of using the File resource:

```powershell Configuration FileExample {

   Import-DscResource -ModuleName PSDesiredStateConfiguration
   Node "Localhost" {
       File ExampleFile {
           DestinationPath = "C:\Temp\example.txt"
           Contents = "Hello, DSC!"
           Ensure = "Present"
       }
   }
}

FileExample -OutputPath “C:\DSC\Configurations” Start-DscConfiguration -Path “C:\DSC\Configurations” -Wait -Verbose ```

  1. Custom Resources

You can create custom resources to extend DSC capabilities. Custom resources are typically defined using PowerShell script modules.

  1. Modes of Operation

- **Push Mode**: Configurations are pushed to target nodes manually using the `Start-DscConfiguration` cmdlet. - **Pull Mode**: Target nodes pull configurations from a central server (pull server) at specified intervals. This requires setting up a pull server and configuring the LCM on each node to use it.

  1. Benefits of PowerShell DSC

- **Consistency**: Ensures that the configuration of systems is consistent across environments. - **Automation**: Automates the deployment and management of configurations, reducing manual effort. - **Compliance**: Helps maintain compliance by ensuring systems stay in the desired state. - **Scalability**: Scales well across large environments with multiple nodes.

  1. Example Use Cases

- **Infrastructure as Code**: Defining and managing infrastructure configurations programmatically. - **Configuration Drift**: Detecting and correcting configuration drift to maintain desired state. - **Software Deployment**: Automating the deployment and configuration of software applications.

PowerShell DSC is a powerful tool for managing infrastructure as code, enabling administrators to ensure their systems are configured correctly and remain in the desired state.


“DSC is a management platform in Windows PowerShell that enables deploying and managing configuration data for software services and managing the environment in which these services run. DSC provides a set of Windows PowerShell language extensions, new Windows PowerShell cmdlets, and resources that you can use to declaratively specify how you want your software environment to be configured. It also provides a means to maintain and manage existing configurations.”

External sites