Implementing Get-TargetResource, Set-TargetResource, and Test-TargetResource

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Vasken Vasken 1 year, 3 months ago.

  • Author
  • #27820
    Profile photo of Vasken

    Greetings, I'm currently doing my best to put the final touches on my first attempt at working with DSC Resources. At this point I feel pretty comfortable with PowerShell but as far as DSC is concerned I'd really like to get input from others as to how to proceed with the situation I've found myself in.

    I decided to take a look at xFirefox and xChrome (both on PowerShell Gallery) as starting points for what a DSC Resource would look like. Using these as a starting point, I was able to write modules with Configuration blocks for installing other software like 7Zip, Thunderbird, and Nmap. I got to the point where the DSC Resources have the correct file structure (schema.mof files in the correct place, psd1 files where required). I'm able to take all my modules are list them via Get-DSCResource and confirm that they are the correct version and name. (I have the code available for all of these and they were previously on PowerShell Gallery before I had to unlist them to fix what I will mention below).

    I got notice from PowerShell Gallery that my modules need to be improved by adding the functions Get, Test, and Set-TargetResource. After further research I now understand that these are a fundamental component of DSC Resources, and I understand that these are skeleton functions that need to be filled out for each DSC Resource.

    What I am trying to understand is why the xFirefox and xChrome modules don't have these functions in the psm1 file if it's required for all DSC resources. I was also told that one of the errors in my module is "The DSC engine could not load the module 'xPSDesiredStateConfiguration'. " which I thought was interesting considering that both xChrome and xFirefox import this module as part of their config. In any case I'm trying to figure out why these two DSC Resources provided by the PS team are more correct in the first place when they don't even contain a schema.mof file, don't contain the three mentioned functions, and when they should also return the same error about importing xPSDesiredStateConfiguration (I'm assuming the device or person auditing the code does not have the module in any of their module paths)

  • #27925
    Profile photo of Don Jones
    Don Jones

    Well... your modules don't need to be "improved" by adding Get- Set- and Test-; those are a _hard requirement_ for DSC. In other words, when the Local Configuration Manager loads your module, it's built to run the Get- Set- and Test- functions. If you don't have those functions, it has nothing to run. We've got a stripped-down example in "The DSC Book" that you can look at, too.

    xChrome and xFirefox are called "Composite Configurations." They're actually not good examples of writing a resource, because they aren't actually resources. They're configurations scripts, saved in a way that makes them usable as a resource by other configuration scripts. xChrome, for example, uses the xRemoteFile and the Package resource, which in turn implement Get-, Set-, and Test- functions. Think of them as "mini configurations" which get included in a regular configuration.

    I'd suggest looking at a true resource, like xDefender, if you want a working example of a full resource.

  • #27930
    Profile photo of Vasken

    Thanks, Don. The distinction you made between Composite Configurations and Resources is what I was looking for. Also I understand "improve" wasn't the right word to use since these functions are required for all Resources. I think one thing that further confused me was that both xChrome and xFirefox have a DSCResources directory as part of their structure just like DSC Resources do.

    I guess the correct term for the modules I've produced is "Configuration Scripts" and not Resources. I'll remove the schema.mof from the module since it doesn't look to be a part of Configuration Scripts and hopefully I can have better luck keeping these listed this time around.

  • #27959
    Profile photo of Vasken

    I removed the schema.mof file and structured my Configuration Scripts properly in the latest version of the modules (tree output below). I also included a sample installer .ps1 script in an Example directory to match how xChrome and xFirefox do it.

    This time I got a notice from the PowerShell Gallery Analyzer that the issue in my script is "Unable to load module 'xPSDesiredStateConfiguration': This is the only issue reported this time around so I feel like I have resolved my confusion about Config. Scripts vs Resources.

    However, I am still trying to figure out why this issue is reported for my modules when other configuration scripts import it as well. Again, xChrome and xFirefox are two examples that are currently on PowerShell Gallery that load xPSDesiredStateConfiguration as part of their code and I would imagine they should return the same error if the analyzer were to analyze them.

    Thanks for taking the time to read this.

        |   xNmap.psd1
        |   \---VH_xNmap
        |           VH_xNmap.psd1
        |           VH_xNmap.schema.psm1

You must be logged in to reply to this topic.