If you"re working with PowerShell modules and need an efficient way to distribute them, using the GitLab Package Registry is a great option. In this guide, we"ll walk through the steps to create, package, publish, and install a PowerShell module using NuGet and GitLab.
The PowerShell module in this example provides a simple function to display the hostname of the current machine.
Context: This guide assumes you are using a self-managed GitLab Community Edition instance (v17.1.1), PowerShell Core Edition 7.5.0, and nuget.exe CLI version 6.13.2.
First, we need to create a module manifest that defines the properties of our PowerShell module. Run the following command:
New-ModuleManifest -Path Show-Hostname.psd1 -RootModule Show-Hostname.psm1 -ModuleVersion "1.0.0" -Author "Simon Bouvet" -Description "A simple PowerShell module to display the hostname"Once the module files are ready, use nuget.exe to package them:
nuget.exe pack Show-Hostname.nuspecThis command generates a .nupkg file, which we can now publish to GitLab.
Before publishing, we need to register GitLab as a NuGet source:
nuget.exe source Add -Name "GitLab" -Source "https://<your_gitlab_instance_fqdn>/api/v4/projects/<your_project_id>/packages/nuget/index.json"To push the package to GitLab, run:
nuget.exe push Show-Hostname.1.0.0.nupkg -Source "GitLab" -ApiKey "<GITLAB_TOKEN>"Replace <GITLAB_TOKEN> with your personal access token from GitLab.
To make PowerShell recognize our GitLab NuGet repository, register it with:
Register-PSResourceRepository -Name "GitLabNuget" -Uri "https://<your_gitlab_instance_fqdn>/api/v4/projects/<your_project_id>/packages/nuget/index.json" -TrustedVerify the registration:
Get-PSResourceRepositoryYou should see an entry for GitLabNuget.
To confirm the module is available, run:
Find-PSResource -Name Show-Hostname -Repository "GitLabNuget"To install the module from GitLab"s NuGet registry, use:
install-PSResource -Name Show-Hostname -Repository "GitLabNuget"After installation, check where the module is stored:
(get-module -Name Show-Hostname).PathIf the module"s path is not in $env:PSModulePath, manually import it:
import-Module -Name Show-HostnameTest the module by running:
Show-HostnameExpected output:
Hostname: mypc