Yesterday I showed you a class-based PowerShell script. My intention was to have a little bit of fun and teach you the basics of using a class. But what I gave you was really just the first step. If you wanted to create an actual tool around a class, you will most likely want to package it into a module. I've done that with my Christmas class. Let me explain why and the changes I made.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
First off, you'll want to get a copy of my module. I have put it on GitHub here. You can clone or download the project. If you want to test it as a module, extract the contents to C:\Program Files\WindowsPowerShell\Modules with a directory name of PSChristmas. You can use any other directory but then you'll need to manually import the module.
The root module primarily contains the modified class definitions. Because I don't want to force the user of the module to know how to work with objects, I created a number of helper functions in a separate file. Some of them are exported in the module. I also moved some of the code from the class methods into separate functions. There is something of an art here in deciding what needs to be a class method and what can be an external function. I've tried to give you a taste of both.
Another advantage for separating out PowerShell code from the class methods is that it makes it easier to develop and test without having to modify the class definition. This makes it easier to use tools like Pester. I've included a sample Pester test to demonstrate the concept.
I also wrote a function to instantiate or create an instance of the class. Again, you don't want to force the user to have to understand the New() method or how to use New-Object. But using an external function it can be documented and discovered. This is especially useful if your class constructor contains overloads as you can parameterize your function and hide the messy bits from the user. Or yourself.
The last major change, again as much for educational purposes as anything, was to move many of the text lists I was using into a json file. This file in imported in the module and turned into an object. The different functions that use the data, then use this object. Separating out the data means I can update the json file without having to go back and modify the function. I also moved one of the enumerations I was using to the json file. Primarily because enumerations don't like spaces in the values and using the json file allowed me to have more reader-friendly values.
I don't want to spoil too much of the fun so I hope you'll grab a copy of the module and try it out. Dig into the code and see if you and discover what I did and why. If you have any questions or problems, please post an issue in the GitHub repository.
Mele Kalikimaka.
Update December 9, 2017
Since I originally wrote this I have made some tweaks to the module and published it to the PowerShell Gallery. Run: Install-Module PSChristmas
Use the Issues section of the GitHub repository to report any problems, questions or suggestions.