PowerShell Desired State Configuration (DSC)

Windows PowerShell 4.0Windows PowerShell Desired State Configuration DSC

Here is an introduction to the main new feature of PowerShell 4.0, Desired State Configuration.  See how to create 'Nodes' and 'Resources', which will manage your operating system.

PowerShell 4.0 New Feature DSC

 ♦

PowerShell Desired State Configuration (DSC)

DSC is Microsoft's new system for building identical servers using PowerShell.  It works well if you want a group of servers with the same role, for example file servers.  As with many PowerShell tasks, the benefit is that automation will save time and produce consistency. 

Another obvious way in which DSC repays your efforts is if you wish to return a server to its default state following a configuration failure of some sort.

 Note 1: DSC depends on Windows PowerShell remoting.

Enable-PSRemoting -Force

Before you deploy a configuration naturally you will need to create a script.  If you have experience of creating a PowerShell function then the format will seem familiar;  The term 'Configuration' replaces 'Function', there are parameters at the top, followed by a process, or in this case a 'Node'.

As the name suggests, the keyword in DSC is Configuration.  Within each script is at least one Node block, for example, HyperV.  The modular nature of DSC expects a resource block for items such as WindowsFeatureIIS.

Example Script for DSC

Configuration HyperVSrv
{
# Parameters
Param(
$MachineName,$WebsiteFilePath
        )
# A Configuration expects at least one Node
Node $MachineName
      {

       # Set DCM Settings for each Node
       LocalConfigurationManager
            {
             RebootNodeIfNeeded = $True
             ConfigurationMode = "ApplyAndAutoCorrect"
             ConfigurationModeFrequencyMins = 15
            }
      #Install the Hyper-V Role
       WindowsFeature HyperV
             {
             Ensure = "Present" 
             Name = "Hyper-V"
              }  # … Add more roles
         } #End of Node
} # End of Configuration

Note 2: To maintain the 'Desired State' sometimes you need to uninstall the role.  To remove a role change Ensure = "Present" to = "Absent"

Note 3: Once again, knowledge of functions is useful when it comes to invoking the Configuration block.

# DSC Example:
MyWebConfig -MachineName "TestMachine" –WebsiteFilePath"\\filesrv\WebFiles" -Output Path "C:\DSC"

Note 4: The result is a MOF file known as the configuration instance document, created at the path specified in the script.

Note 5: DSC runs every 15 minutes if you are employing a push technology.  It works rather like a Group Policy Object, where you push out settings which the operating systems enforces.

# Run your script with:
Start-DscConfiguration -Path "C:\DSC" -Wait -Force -Verbose
# Check with Get-DscConfiguration and Test-DscConfiguration.

Note 6: When Start-DscConfiguration runs it calls the Local Configuration Manager (LCM) on the server, which then process the MOF files.  It is the LCM that makes any changes to enforce the assigned configuration.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

PowerShell DSC in Action

Windows PowerShell 4.0 DSC

Obtaining Windows Management Framework 4.0

Microsoft provides the Desired State Configuration (DSC) as part of .NET Framework.  This is the situation: you get PowerShell version 4.0 automatically with Server 2012 R2.

However, if you have Windows Server 2012 or 2008 R2, then you can install Windows Management Framework 4.0

See what else is new in PowerShell 4.0 »

Summary of PowerShell's Desired State Configuration

Microsoft's DSC is the latest management system that enables the deployment and management of configuration data for software services and the environment on the machines which these services operate.

The configuration is much like a PowerShell function, it has parameters, and the Process section is replaced by Nodes and Resources.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell Examples of Real Life Tasks

PowerShell Real-life Examples   • Test-Connection   • Invoke-Expression   • Invoke-Command

Windows PowerShell   • Trace-Command   • PowerShell Registry MaintainServerList

PowerShell Registry   • Compare-Object Registry   • Desired State Configuration   • Get-Hotfix

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.