Last week I posted a Friday Fun article about using PowerShell to create a synopsis for a hypothetical thriller novel. Naturally I wasn't satisfied to leave it at that. Don't get me wrong, it was a good start. But I needed to take the next logical step. I had a script, but that meant having to type the full path to the script. It would have been better if I had a function. I already had an internal function, which would need to remain hidden. Plus all of the content was embedded in the script. It would be better to turn this into a module.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
It was pretty easy to begin. I renamed the initial script file with a psm1 file extension. I wrapped the main code into a function and moved the internal, helper function outside of the main function. The only other thing I need to do was use Export-ModuleMember to make the function visible.
Export-ModuleMember -Function New-Thriller
Now I could import the module and have a command to run. The data was still internal which meant that if I wanted to add new titles or names to the lists I had to modify the file. I decided the easiest solution was to move the data to an external json file. This way I could import the file as an object.
This also meant I could use the data in other ways, such as creating a command to get one or more titles.
PS C:\> Get-PSThrillerTitle -Count 3 The Drive-In Thunderstruck No Yesterdays
Or create a characters object.
PS C:\> Get-PSThrillerCharacters Hero : Jim Shorts FormerOccupation : Marine TheWoman : Mary Ann HerStory : a cocktail waitress Villain : The Shadow
I've gone through the json file and added even more potential values. I also added some fake testimonials from other authors.
Along the way, I renamed the noun in my commands to PSThriller and created a manifest. I also used the Platyps module to create help for the exported commands.
The primary purpose for all of this work is education. I want you to be able to see how a module goes together. I want you to know how you can have a mix of private and public functions. I want you to see how you can integrate a data source into your commands.
I don't plan on publishing this to the PowerShell Gallery since it isn't really a practical module. But I hope you will go the project's GitHub repo at https://github.com/jdhitsolutions/PSThriller and check out the code. There is a release zip file you can download if you wish. The README.md file has additional information.
Have fun and watch your six!