Divvying Up PowerShell Class-Based Resources in .psm1 Modules

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Dave Wyatt Dave Wyatt 8 months, 1 week ago.

  • Author
    Posts
  • #37212
    Profile photo of Ryan Spletzer
    Ryan Spletzer
    Participant

    I'm currently writing a module with class-based DSC resources.

    All the examples I've seen out there involve putting all the class-based DSC resources for a module in the .psm1 file, which is all fine and dandy, except that this code file gets very long after a while...

    I seem to remember in the past with a preview of WMF 5 trying to put the class-based resources in their own separate ps1 files that the psm1 would load, but this did not seem to work. I have not tried again with WMF 5 RTM (yet).

    Does anyone know if there is a proper / supported way to divvy up class-based DSC resources into their own files? Or for this approach is the only option to have them all inside the .psm1 file?

  • #37214
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Not yet, but I believe this is intended for an upcoming update to WMF5 (possibly already there for people on the Windows 10 fast track / insider ring / whatever it's called.)

    Eventually, what you'll be able to do is just list your modules in the NestedModules key of the module manifest, and you can divide up the DSC resources however you like inside the psm1 files that you put in NestedModules. There was even some documentation to that effect that leaked out by mistake, before the necessary LCM changes were actually released.

  • #37258
    Profile photo of Arie H
    Arie H
    Participant

    Its all text files at the end.

    You can always build yourself a small script that grabs other small scripts that will be easier to maintain for you and create the PSM file from a template you prepared in advance.

    Just have to implement some checks and even pester if youre into it to make sure the PSM is in the desired "state".

  • #37269
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    "Its all text files at the end.

    You can always build yourself a small script that grabs other small scripts that will be easier to maintain for you and create the PSM file from a template you prepared in advance."

    That's the way modules used to work, for the most part. With classes, some of this stuff happens at parse time without actually importing the module, so PowerShell and the LCM have to be able to find the classes in the expected files. It does this by looking at the files explicitly mentioned in the manifest (either the RootModule or, eventually, NestedModules.)

You must be logged in to reply to this topic.