PowerShell Desired State Configuration (DSC)

PowerShell Desired State Configuration (DSC) is a management platform that allows administrators to configure and manage their IT infrastructure in a declarative way. It is a component of PowerShell that can be used to ensure that server configurations are consistent and predictable across a large number of servers.

Features and Functions: PowerShell DSC allows administrators to define the desired state of a server or application, and then automatically configure that server to meet that state. Some of the features and functions of PowerShell DSC include:

  1. Configuration as Code: PowerShell DSC allows you to define the configuration of a server or application as code, which can be stored in source control and easily shared with other administrators.
  2. Consistency: PowerShell DSC ensures that the configuration of a server or application is consistent across all servers, which reduces errors and simplifies management.
  3. Automation: PowerShell DSC allows you to automate the configuration of servers or applications, which reduces the time and effort required to configure them manually.
  4. Idempotency: PowerShell DSC ensures that the configuration of a server or application is idempotent, which means that the same configuration can be applied repeatedly without causing unintended changes.

Installation and Configuration: PowerShell DSC is included in Windows Management Framework 5.0 and later versions of Windows Server. It can be installed and configured using the following steps:

  1. Install PowerShell DSC by installing Windows Management Framework 5.0 or later on the target servers.
  2. Create a configuration file that defines the desired state of the server or application. This can be done using PowerShell cmdlets or by writing a PowerShell script.
  3. Apply the configuration file to the target servers using PowerShell DSC cmdlets. This can be done manually or using a configuration management tool like Microsoft System Center Configuration Manager.

Usage Examples: Here are a few examples of how PowerShell DSC can be used:

  1. Configuring IIS: PowerShell DSC can be used to configure the Internet Information Services (IIS) web server on a large number of servers. The desired state of the server can be defined in a configuration file, and then applied to all servers using PowerShell DSC cmdlets.
  2. Managing User Accounts: PowerShell DSC can be used to manage user accounts on a large number of servers. The desired state of the user accounts can be defined in a configuration file, and then applied to all servers using PowerShell DSC cmdlets.
  3. Installing and Configuring Applications: PowerShell DSC can be used to install and configure applications on a large number of servers. The desired state of the application can be defined in a configuration file, and then applied to all servers using PowerShell DSC cmdlets.

In summary, PowerShell DSC is a powerful management platform that allows administrators to configure and manage their IT infrastructure in a declarative way. It provides consistency, automation, and idempotency, which can help reduce errors and simplify management. PowerShell DSC is included in Windows Server and can be installed and configured using a few simple steps.

Example powershell dsc script ensure webserver and hyper-v is present on serverr host1

Here’s an example PowerShell DSC script that ensures that the Web Server and Hyper-V roles are installed on a server named “host1”:

Configuration ServerConfig
{
    Node "host1"
    {
        # Ensure Web Server role is installed
        WindowsFeature WebServer
        {
            Ensure = "Present"
            Name = "Web-Server"
        }

        # Ensure Hyper-V role is installed
        WindowsFeature HyperV
        {
            Ensure = "Present"
            Name = "Hyper-V"
        }
    }
}

# Generate MOF file for the configuration
ServerConfig -OutputPath "C:\DSC\Configs"

# Apply the configuration to the target node
Start-DscConfiguration -Path "C:\DSC\Configs" -ComputerName "host1" -Verbose

This script defines a configuration named “ServerConfig” that targets a node named “host1”. Within the node block, it uses the WindowsFeature resource to ensure that the Web Server and Hyper-V roles are installed on the server. The Ensure property is set to “Present” to ensure that the roles are installed, and the Name property specifies the name of the roles.

Once the script is run, a MOF file is generated for the configuration and stored in the “C:\DSC\Configs” directory. The Start-DscConfiguration cmdlet is then used to apply the configuration to the target node (“host1”). The -Path parameter specifies the path to the MOF file, and the -ComputerName parameter specifies the target node.

When the script is run on “host1”, it will ensure that the Web Server and Hyper-V roles are installed on the server if they are not already present. If they are already installed, the script will have no effect.

Author: tonyhughes