Debug Mode in Desired State Configuration

Debug Mode in Desired State Configuration

Rate This
  • Comments 1

The DSC engine caches resources implemented as a PowerShell module for efficiency purposes. This can sometimes turn out to be annoying, when you are authoring a resource and testing it simultaneously. The only way to cause DSC to load the newer version every time is to explicitly kill the process hosting the DSC engine.

As part of the WMF5 CTP release, we have introduced a new property for configuring the DSC Local Configuration Manager (LCM) called the DebugMode. When set to true, it causes the engine to reload the PowerShell DSC resource. Once you are done writing your resource, you can set it back to false and the engine will revert to its behavior of caching the modules.

Here is a demonstration of using the DebugMode:

PS C:\Users\WinVMAdmin\Desktop> Get-DscLocalConfigurationManager

 

 

AllowModuleOverwrite           : False

CertificateID                  :

ConfigurationID                :

ConfigurationMode              : ApplyAndMonitor

ConfigurationModeFrequencyMins : 30

Credential                     :

DebugMode                      : False

DownloadManagerCustomData      :

DownloadManagerName            :

LocalConfigurationManagerState : Ready

RebootNodeIfNeeded             : False

RefreshFrequencyMins           : 15

RefreshMode                    : PUSH

PSComputerName                 : 

 

To begin with, it is false (by default).

For the purpose of demonstration, we will be using the following PowerShell resource:

function Get-TargetResource

{

    param

    (

        [Parameter(Mandatory)]

        $onlyProperty

    )

    return @{onlyProperty = Get-Content -path "$env:SystemDrive\OutputFromTestProviderDebugMode.txt"}

}

function Set-TargetResource

{

    param

    (

        [Parameter(Mandatory)]

        $onlyProperty

    )

    "1"|Out-File -PSPath "$env:SystemDrive\OutputFromTestProviderDebugMode.txt"

}

function Test-TargetResource

{

    param

    (

        [Parameter(Mandatory)]

        $onlyProperty

    )

    return $false

}

 

We author a configuration using the above resource(let us call the resource TestProviderDebugMode):

Configuration ConfigTestDebugMode

{

    Import-DscResource -Name TestProviderDebugMode

    Node localhost

    {

        TestProviderDebugMode test

        {

            onlyProperty = "blah"

        }

    }

}

ConfigTestDebugMode

 

 If we read the contents of the file: “$env:SystemDrive\OutputFromTestProviderDebugMode.txt”, we see it is 1. Now, let us update our provider code. For the purpose of demonstration, we will use the following script:

$newResourceOutput = Get-Random -Minimum 5 -Maximum 30

$content = @"

function Get-TargetResource

{

    param

    (

        [Parameter(Mandatory)]

        `$onlyProperty

    )

    return @{onlyProperty = Get-Content -path "$env:SystemDrive\OutputFromTestProviderDebugMode.txt"}

}

function Set-TargetResource

{

    param

    (

        [Parameter(Mandatory)]

        `$onlyProperty

    )

    "$newResourceOutput"|Out-File -PSPath C:\OutputFromTestProviderDebugMode.txt

}

function Test-TargetResource

{

    param

    (

        [Parameter(Mandatory)]

        `$onlyProperty

    )

    return `$false

}

"@ | Out-File -FilePath "C:\Program Files\WindowsPowerShell\Modules\MyPowerShellModules\DSCResources\TestProviderDebugMode\TestProviderDebugMode.psm1"

 

 

It generates a random number and updates the provider code accordingly. This simulates the scenario where you need to make frequent updates to your provider and test them.

With the DebugMode set to false, the contents of the file “$env:SystemDrive\OutputFromTestProviderDebugMode.txt” are never changed.

Now let us set the DebugMode to true by having the following block in our configuration script:

LocalConfigurationManager

{

    DebugMode = $true

}

 

Now, if you run the above script again, you will see that the content of the file is different every time. (You can do a Get-DscConfiguration to check it). Here is what I got in two subsequent runs:

PS C:\Users\WinVMAdmin\Desktop> Get-DscConfiguration -CimSession (New-CimSession localhost)

 

onlyProperty                            PSComputerName                        

------------                            --------------                        

20                                      localhost                             

 

PS C:\Users\WinVMAdmin\Desktop> Get-DscConfiguration -CimSession (New-CimSession localhost)

 

onlyProperty                            PSComputerName                        

------------                            --------------                        

14                                      localhost                             

 

 

Happy configuring!

Abhik Chatterjee

Windows PowerShell Developer

 

Leave a Comment
  • Please add 5 and 6 and type the answer here:
  • Post
  • A VERY welcome addition, killing the provider wasn't exactly hard but it also wasn't exactly intuitive. Nice!

Page 1 of 1 (1 items)