Question

I am debugging PowerShell DSC resources that come with v4.0. More specifically, I am testing MSFT_ProcessResource by adding a diagnostic log. After I make change to the resource, and run my configuration that exercise the resource, I don't see the logging I just added. Eventually after several minutes, PowerShell seems to refresh whatever cache of resource it has. I've tried Get-DscResource, and Import-Module MSFT_ProcessResource

None of which worked.

Is there a way to force re-loading the resource?

Was it helpful?

Solution

DSC engine caches resources to increase performance.

There are two ways to reload the resource:

1) Restart process hosting DSC engine (kill WMI Provider Host and re-run the configuration)

2) Use debug mode which will cause DSC to reload resources automatically (useful when developing resources, but not recommended for regular work):

LocalConfigurationManager
{
    DebugMode = $true
}

You can read more about debug mode here: http://blogs.msdn.com/b/powershell/archive/2014/04/22/debug-mode-in-desired-state-configuration.aspx

OTHER TIPS

  • DSC has a caching model which frankly seems buggy and poorlyl designed as of Sep 2016
  • The blog entries indicating the mechanisms to get around the caching don't always work
  • In your configuration include the following configuration line
  • Also perform a full restart of the winmgt service. Simply killing the dsctimer process doesn't appear to always work.

{ LocalConfigurationManager { DebugMode = "All" } }

A PowerShell script to clear the cache is:


$dscProcessID = Get-WmiObject msft_providers | 
   Where-Object {$_.provider -like 'dsctimer'} | 
   Select-Object -ExpandProperty HostProcessIdentifier 

if ($dscProcessID -eq $null) {
    Write-Host "DSC timer is not running."
    return
}
Write-Host "Process ID: $dscProcessID"

Get-Process -Id $dscProcessID | Stop-Process -Force

Restart-Service -Name winmgmt -Force -Verbose

This has now changed with WMF 5, instead of $true debug mode has the following options.

  • None - Signifies that DebugMode is False and not applicable.
  • ForceModuleImport - Enforce the resource module to be reloaded instead of using the cache. This is similar to "true" value in previous versions.
  • ResourceScriptBrealAll - Helps in debugging DSC resources when Local configuration manager tries to execute their functions. More on it in subsequent blog posts!
  • All - Signifies that debugging as well as reloading of modules are both enabled.

Using this in an example DSC config would look like this:

Configuration myChocoConfig2
{
   Import-DscResource -Module cChoco  
   Node "localhost"
   {
      LocalConfigurationManager
      {
          DebugMode = 'All'
      }
      cChocoInstaller installChoco
      {
        InstallDir = "c:\choco"
      }
      cChocoPackageInstaller installChrome
      {
        Name = "sysinternals"
        DependsOn = "[cChocoInstaller]installChoco"
      }

   }
} 

https://techstronghold.com/blogs/scripting/how-to-setup-debug-mode-in-windows-powershell-desired-state-configuration-dsc

I have a set of scripts that load on start of PowerShell and I often needed the same. I would edit one of my scripts and need it to be updated in the current session.

Because I have these scripts loading via a series of scripts in the $profile I am able to use one command to refresh for any of the scripts that I load on init.

C:> powershell

This command will refresh your session and keep you in the same folder you are currently in. If you are not loading your module on startup, you will need to use the answer from Karol.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top