Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

A Classy Christmas PowerShell Module

Posted on December 20, 2016December 9, 2017

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.

Manage and Report Active Directory, Exchange and Microsoft 365 with
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.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d