0

I have a module where I define enumerators. Currently, I have the enumerator definitions in a ps1 file, which I dot-source in the psm1 module file, like this:

Enumerators.ps1

enum Fruits {
    apple
    pear
    orange
    banana
}

MyModule.psm1

. Fruits.ps1

I was doing the same with classes, but I learned about the Types.ps1xml file for defining types, so I'll be moving my class definitions into there. Is there a comparable method for defining enumerators? Microsoft's documentation doesn't mention an "enumerator" member tag for Types.ps1xml.

I found this question, which is from PS v5--I'm wondering if something's changed with PS6/7?

jeremywat
  • 181
  • 7
  • Can you clarify on what you mean by moving the class definition to the types file? The `types.ps1xml` is for adding ETS members to a type not a place where you would store a type definition – Santiago Squarzon Aug 22 '23 at 18:04
  • In that case, maybe I misunderstood the purpose of `types.ps1xml`. So, does that mean that dot-sourcing for enums and classes is still the best method? – jeremywat Aug 22 '23 at 18:09
  • The reco I would give you is to if you want to have separated ps1 files then use a module that would merge all files into a single psm1 file. You can look into ModuleBuilder for example. You will also want to have a build file for your module with steps for building and testing and for that you can look into `Invoke-Build` – Santiago Squarzon Aug 22 '23 at 18:11
  • 2
    "So, does that mean that dot-sourcing for enums and classes is still the best method?" personally, I would merge all files into a single `psm1` but that is an alternative. The common pattern is to have a module folder where you have a private and public folder and a psd1 (no psm1) then ModuleBuilder targets that folder and merges all files into a single `psm1` with its corresponding `psd1`. This process is all done by your `build.ps1` file and invoked by your yaml pipeline at the moment of publishing to the PS Gallery or whatever – Santiago Squarzon Aug 22 '23 at 18:14

0 Answers0