Build and Publish a Powershell Module to the Powershell Gallery
In this blog we will dive into what is a Powershell Module, how its changed and then what can we do to build and publish the module to the Powershell Gallery. I recently did this presentation again at VMworld Explore 2022 and it was a hit, so I figured its finally time to get it here on the blog so it can be referenced and viewed year round.
About Powershell Modules
If we think back to the old days whenever we wanted to use a module it required having to manually go out and download it and import it at the beginning of every script. This was extremely problematic when using things like PowerCLI where every time you wanted to run a cmdlet you had to import the module.
In my presentation I like to say the future has happened. With the ability to install Powershell Modules from the Powershell Gallery you no longer need to manually import them. As the cmdlets are needed they are automatically loaded from the installed module list.
What is also important is there is a build in Update-Module
cmdlet that will go out and automatically find the latest version and install it. Just be aware that when this happens it will also keep previous versions around. If you wish to only keep the latest version make sure to uninstall/remove the previous versions as well.
How to Build a Powershell Module
When I think about building a module what I first do is see if what I want to do already exists. If it doesn’t then I think about what I want to do. I can write a script, write a function and put it into a module.
If we dont know the code we want to write, we can use some of the built in developer tools in the vSphere Client like API Explorer or Code Capture. Code Capture allows you to record UI based tasks and get an output in GO, Javascript and Powershell. If there is no output, it means its most likely a private API or one does not exist.
How to Publish a Powershell Module
As we think about publishing the module, the first thing we need to do is create a module manifest. This is a description of all details in the module such as Author, Requirements and Versioning.
To create the module manifest we can use the New-ModuleManifest
cmdlet to build the file, here is an example that I have used.
1$ModuleSettings = @{
2 RootModule = './demo-module-explore22.psm1'
3 ModuleVersion = '2.0.0'
4 Author = 'David Stamen'
5 Description = 'This is a demo created for VMware Explore 2022'
6 FunctionsToExport = "Get-VMClusterInfo"
7 IconURI = 'https://github.com/dstamen/demo-module-explore22/raw/main/logo.png'
8}
9
10New-ModuleManifest @ModuleSettings -Path './demo-module-explore22.psd1' -PassThru
Once we have the manifest created we can use the Test-ModuleManifest
cmdlet to verify its compatible and there are no errors.
1Test-ModuleManifest -Path ./demo-module-explore22.psd1
Once we are done with our manifest, we can get our API Key from the PowershellGallery and then go ahead and publish the module with the following cmdlet.
1$APIKey = 'YOUR-API-KEY'
2Publish-Module -Path . -NuGetApiKey $APIKey
Once its published we can see all the details online in the powershell gallery.
Conclusion
While creating and publishing the module isnt all that hard, make sure if you are making it public there are no issues with what you are sharing. If you wish to make it private, there are many private nuget repositories out there you can use to store it locally for personal or corporate use.
comments powered by DisqusSee Also
- Using Pure1 Meta for Intelligent Datastore Selection and VM Provisioning
- Deploying a EC2 Instance with PowerShell to AWS
- Installing and Updating the Pure Storage vSphere Plugin
- Patching the vCenter Server Appliance (VCSA) using the REST API - Part 2 (PowerCLI Module)
- Using Powershell to Create Windows SMB Shares