top of page
Writer's pictureRattandeep singh

PowerShell Desired State Configuration: The Basics: Part 1



PowerShell Desired State Configuration (DSC) plays a vital role in configuring, managing, and sustaining Windows servers. It empowers PowerShell scripts to define a machine's configuration using a declarative approach, simplifying maintenance and comprehension.
What Is Desired State Configuration (DSC)?

Managing and maintaining servers can quickly become a complex endeavor in the absence of standardization. PowerShell offers various ways to address diverse issues and occasionally circumvent certain restrictive methods. However, this approach can lead to an assortment of complex scripts for server management, each more intricate than the last. It's far more efficient to employ a standardized method for automating tasks.


DSC provides us with a declarative model for system configuration management. In essence, it allows us to specify how we want a workstation or server (referred to as a 'node') to be configured, leaving it to PowerShell and the Windows Workflow engine to execute these configurations on the target 'nodes.' We no longer need to define the specific steps for achieving the desired configuration.

The primary advantages of DSC include:

  1. Simplifying sysadmin tasks by automating the configuration of one or more devices.

  2. Enabling consistent configuration across machines to establish standardization.

  3. Ensuring that a machine's configuration remains identical to its initial setup at any given time to prevent configuration drift.

  4. Streamlining and automating deployment, whether for cloud strategies or large-scale implementations.

DSC encompasses two architectural modes.

Push Mode: In this mode, configurations are manually dispatched or 'pushed' to one or more units referred to as 'nodes.' Administrators initiate this action.


In this design, only unidirectional communication is feasible, with the administrator responsible for dispatching configurations to the machines.

Pros:

  • Cost Efficiency: There's no need to invest in a new server since configurations are sent from your workstation.

  • Architectural Simplicity: The architecture remains straightforward as all configurations are stored on your workstation.

  • Testing Ideal: It's an ideal setup for testing the functionality of "Desired State Configuration."

Cons:

  • Complex Machine Management: Managing connected machines becomes more complex. Due to the intermittent network connectivity of laptops, configuration transmission may encounter challenges.

Pull Mode: In this mode, a 'pull server' is established, and the nodes periodically communicate with this server to retrieve their configurations.


In this architectural setup, it is the nodes that regularly check the "pull server" to inquire about the availability of a configuration. If a configuration is indeed available, the "pull server" then transmits it to compatible machines. By default, this communication interval is set to fifteen minutes.

Pros:

  • Streamlined Configuration Deployment: It automates the deployment of configurations.

  • Effective Management of Multiple Machines: It facilitates the management of numerous machines, whether they are currently connected to the network or not. As soon as a machine reconnects to the network, it requests its configuration from the "Pull Server."

Cons:

  • Requirement for an Additional Server: You need to deploy an extra server.

What can be managed by PowerShell DSC?

There are multiple resources available to be managed in DSC known as DSC resources. We can list those in the PowerShell console using the command Get-DscResource.


What to do with these resources?

A DSC resource is essentially a piece of PowerShell code that defines and configures a specific aspect of a system. It encapsulates the logic needed to bring a system into compliance with a desired state.

In simple words, DSC resources are the components used to declare what should be configured. I will demonstrate this with an example of the first resource i.e. File.


Run command "Get-DscResource File -Syntax" to show the details as below.

It is a syntax, that can be used to perform different operations as per the requirements for the file resources. I will start with a very simple example here.

As an administrator, we need to have a file named "EssentialFile.txt" always at location "C:\Temp".


There are a few steps to achieve this in PowerShell DSC.

  • Create a new PowerShell Script using the File resource parameters like

    1. DestinationPath - This is to declare the path where the file should be available

    2. Ensure - I will use Present, as its value to make sure this file is available

    3. Contents - This can be used to define the content of the file

The code will look like.

The moment we run the code, a localhost.mof file will be created at the path we added in the script.

  • It is time for the next step to use the .mof file to start or push the DSC configuration by running the command. I have used dir to demonstrate the file was not there before and it appeared after running the configuration.

Start-DscConfiguration "C:\Temp\NewFileByDSC"

What if you need to identify the DSC configuration in plance on the server.

Simply run the command Get-DscConfiguration to show the configuration currently applied.


What if you need to identify the type of DSC configuration referesh mode? Is it Push or Pull?

Simply run the command Get-DSCLocalConfigurationManager to show the configuration currently applied.


It's crucial to explain one more basic aspect of DSC: the 'Local Configuration Manager,' often referred to as LCM, serves as the operational powerhouse of DSC. Operating on every node, it takes on the responsibility of implementing configurations. When a configuration is dispatched to a node, the LCM meticulously scrutinizes the accompanying .mof file. Upon comprehending the file's contents, it proceeds to invoke all available DSC resources on the node, thereby orchestrating the desired configuration adjustments on the machine. Additionally, the LCM plays a pivotal role in monitoring and managing configuration drift to ensure ongoing compliance with the specified settings.


Let me summarize the steps to create push a Powershell DSC configuration.
  1. Identify the DSC resource you need to work with

  2. Check the syntax of the DSC resource

  3. Create a script using the chosen parameters/options

  4. Execute the script to create the .mof file

  5. Start or Push the DSC configuration using Start-DscConfiguration command

By now, you must be thinking can we revert this file creation if needed?

Of course, we can do it with another configuration push. I will share the script below and leave it for you to try and see if it works.


I trust that this article has enriched your understanding of DSC. We've primarily delved into the "Push mode" as it offers a straightforward and swift approach to harnessing DSC's capabilities. However, in a forthcoming article, we will explore why the "Pull mode" is more suitable for production environments.

To be continued..
42 views0 comments

Comments


bottom of page