Thursday, March 12, 2015

Desired State Configuration–ConfigurationMode

Working on easing my way into running DSC on some FIM servers and want to tell the Local Configuration Manager (LCM) to test the objects but not set them.

DSC allows you to configure the LCM with a property named ‘ConfigurationMode’ which accepts values of:

  • ApplyOnly – Configuration is applied once
  • ApplyAndMonitor – Configuration is applied, and LCM monitors whether any changes occur.  If configuration drift is detected, it is reported in the logs.
  • ApplyAndAutoCorrect – Configuration is applied, and LCM monitors whether any changes occur.  If configuration drift is detected, LCM enforces the configuration.

ConfigurationMode is then useful for previewing what changes DSC wants to correct.

There is a slight gotcha here, in that ConfigurationMode is ignored by Start-DscConfiguration.  You might notice this if you push a configuration and watch it run successfully, then when it runs in the background thereafter it never corrects any objects.  I found this a little confusing.

Here’s a script snippet to observe the behaviour.  The configuration uses a Script resource (one of the built-in resources) to write messages (it doesn’t actually change anything).   It is a harmless way to see how the LCM behaves when ConfigurationMode is set to different values.

 

configuration DscTest

{

    node (hostname)

    {

        LocalConfigurationManager

        { 

             <#

             Specifies how often (in minutes) LCM ensures that the configuration is in the desired state.

             #>

             ConfigurationModeFrequencyMins = '60'

             <#

             Specifies how LCM should apply the configuration. Valid values are:

                · ApplyOnly:           Configuration is applied once.

                · ApplyAndMonitor:     Configuration is applied, and LCM monitors whether any changes occur. If configuration drift is detected, it is reported in logs.

                · ApplyAndAutoCorrect: Configuration is applied, and LCM monitors whether changes occur. If configuration drift is detected, LCM reapplies the configuration.

             #>

             ConfigurationMode = 'ApplyAndMonitor'

        }

 

        Script DscTestScript

        {

            GetScript  = {Write-Verbose "Got it."}

 

            TestScript = {Write-Verbose "Just Testing.";return $false}

 

            SetScript  = {Write-Warning "You won't see this if ConfigurationMode is ApplyAndMonitor."}

        }

    }

}

 

### Generate the MOF files

DscTest -OutputPath "$env:TEMP\DscTest" -Verbose

 

### Set the LCM settings

Set-DscLocalConfigurationManager -Path "$env:TEMP\DscTest" -Verbose

Get-DscLocalConfigurationManager

 

### Execute the configuration

Start-DscConfiguration -Wait -Path "$env:TEMP\DscTest" -Verbose -Force 

 

 

No comments: