Two Clsss Dsc Resources in one module

Welcome Forums DSC (Desired State Configuration) Two Clsss Dsc Resources in one module

Viewing 7 reply threads
  • Author
    Posts
    • #35394
      Participant
      Topics: 13
      Replies: 14
      Points: 0
      Rank: Member

      I’m trying to have the following folder structure with Dsc Class Resources in DscResource1 and DscResource2. Everything I try with NestedModules, etc.. I keep getting errors from the LCM “Failed to create an object of PowerShell class IssRobocopy”. Everything works fine if I just keep the module name files and the resource name file the same. This seems very restrictive.

      –DscModule
      —DscModule.psd1
      —DscResource1.psm1
      —DscResource2.psm1

    • #35404
      Member
      Topics: 9
      Replies: 2322
      Points: 0
      Rank: Member

      For class-based resources, all of the class definitions need to be in a single psm1 file (which should be referenced by the RootModule element in your manifest, and probably safest to also name it DscModule.psm1). I don’t know if / when that’s going to change.

      However, you can write other PowerShell functions in separate files, and just have the stuff in DscModule.psm1 be the simple shell of the classes (much like a schema.mof file in the old resources). Something like this:

      [DscResource()]
      class DscResource1
      {
          [DscProperty(Key)]
          [string] $Param1
          
          [void] Set()
          {
              Invoke-DscResource1Set -Param1 $this.Param1 # etc
          }
      }
      
    • #35405
      Member
      Topics: 9
      Replies: 2322
      Points: 0
      Rank: Member

      Deliberately left that code short, but you get the idea.

    • #35412
      Participant
      Topics: 13
      Replies: 14
      Points: 0
      Rank: Member

      Thanks, that’s very disappointment limitation of the class dsc model.

    • #35529
      Participant
      Topics: 0
      Replies: 3
      Points: 0
      Rank: Member

      I am not sure if I am getting this right. I want to combine some class based DSC resources in one PowerShell module. Are you saying that this is not possible?

    • #35548
      Member
      Topics: 9
      Replies: 2322
      Points: 0
      Rank: Member

      It’s fine to have multiple classes in one module. The class definitions just all have to be in the same PSM1 file; you can’t currently split them into multiple ps1 files and dot-source them from the PSM1, or anything along those lines.

    • #40287
      GS
      Participant
      Topics: 108
      Replies: 186
      Points: 12
      Rank: Member
    • #94713
      Participant
      Topics: 2
      Replies: 9
      Points: 0
      Rank: Member

      I’m not sure if in the mean time people have worked this out but the way to do this is to create another folder in the root of your module called DSCClassResources. Each Class resource has a module manifest and is referenced as a nested module within the parent module manifest.

      * MyModule
      ** DSCClassResources
      *** Resource1
      **** Resource1.psm1
      **** Resource1.psd1
      ** DSCResources
      *** Resource2
      **** Resource2.schema.mof
      **** Resource2.psm1
      ** MyModule.psd1

Viewing 7 reply threads
  • The topic ‘Two Clsss Dsc Resources in one module’ is closed to new replies.