Docsity

Local PowerShell Module Repository, No Server Required, Lecture notes of Advanced Computer Programming

Toggle sidebar
bg1
Local PowerShell Module Repository, No Server Required
COMMON CHALLENGE WHEN DEVELOPING POWERSHELL MODULES
Simple PowerShell scripts can go a long way on their own, but they can go even further with
PowerShell modules. The largest PowerShell repository is PSGallery, where one can find
modules to get the job done for just about anything. While published modules are relatively
easy to install and import, that is not the case for local, unpublished modules. This is a common
challenge when developing PowerShell modules, but there is hope!
pf3
pf4
pf5

Partial preview of the text

Download Local PowerShell Module Repository, No Server Required and more Lecture notes Advanced Computer Programming in PDF only on Docsity!

Local PowerShell Module Repository, No Server Required

COMMON CHALLENGE WHEN DEVELOPING POWERSHELL MODULES

Simple PowerShell scripts can go a long way on their own, but they can go even further with PowerShell modules. The largest PowerShell repository is PSGallery, where one can find modules to get the job done for just about anything. While published modules are relatively easy to install and import, that is not the case for local, unpublished modules. This is a common challenge when developing PowerShell modules, but there is hope!

Choosing a Reference PowerShell Module

We have chosen the Devolutions.Hub PowerShell module as a reference, but the same steps can be followed using a different module name and version. PowerShell 7 is required to load this cross-platform module, so Windows PowerShell 5.1 (built-in) cannot be used. Refer to the official PowerShell installation instructions, or use the following convenient one-liner on Windows: iex «& { $(irm https://aka.ms/install-powershell.ps1) } -UseMSI -Quiet» Once PowerShell 7 (pwsh.exe) is installed, to avoid issues ensure that it is always used instead of the older Windows PowerShell (powershell.exe).

Importing Modules by Name or Path

Published modules can be installed with the Install-Module command, after which they can be imported with the Import-Module command using the module name: Install-Module -Name ‘Devolutions.Hub’ Import-Module -Name ‘Devolutions.Hub’ Modules can also be loaded using the path to the .psm1 file: $ModuleBase = Get-Module -Name Devolutions.Hub -ListAvailable | Select-Object -First 1 -ExpandProperty ModuleBase Import-Module «$ModuleBase\Devolutions.Hub.psm1»

Verify that the new repository was created successfully with the Get-PSRepository command: PS > Get-PSRepository Name InstallationPolicy SourceLocation


local Trusted C:\Users\wayk\psrepo PSGallery Untrusted https://www.powershellgallery.com/api/v PSGallery is untrusted by default because its contents are not curated. We mark the local repository as trusted for the simple reason that it is local, and therefore not controlled by someone else.

Saving a PowerShell Module

To avoid the trouble of building a PowerShell module from source, let’s grab one from PSGallery. The Save-Module command can be used to fetch a module and copy it to a local path without installing it. To demonstrate how this works, any module can be used as long as it is uninstalled prior to following the instructions. The module can be reinstalled cleanly afterward, so there is no need to worry. $ModuleName = ‘Devolutions.Hub’ $ModuleVersion = ‘2021.1.0’ New-Item -Path «~/modules» -ItemType ‘Directory’ -Force | Out-Null Save-Module -Name $ModuleName -RequiredVersion $ModuleVersion ` -Repository ‘PSGallery’ -Path «~/modules» $ModulePath = «~/modules/${ModuleName}/${ModuleVersion}» The above saves the Devolutions.Hub module from PSGallery to “~/modules/Devolutions.Hub/2021.1.0”. This directory should contain the module manifest file (.psd1).

Loading Modules from PSModulePath

How exactly does PowerShell find modules when importing them by name? It usesPSModulePath, an environment variable containing a list of directories to look into. This is very similar to the PATH environment variable that controls which directories to search for system executables. In fact, PSModulePath uses the same separating character as PATH: ‘;’ on Windows, ‘:’ on non-Windows. By adding “~/modules” to PSModulePath, we can make its modules available to PowerShell. To avoid interference by a previously installed module, ensure the sample module is correctly uninstalled: Uninstall-Module -Name $ModuleName -AllVersions Get-Module -Name $ModuleName -ListAvailable -Refresh Next, temporarily modify PSModulePath, check that the module becomes available, and then revert PSModulePath to its original value: $PSModulePath = $Env:PSModulePath $Env:PSModulePath += «$([IO.Path]::PathSeparator)$(Resolve-Path «~/modules»)» Get-Module -Name $ModuleName -ListAvailable -Refresh $Env:PSModulePath = $PSModulePath The Get-Module command should list the sample module inside the ~/modules directory. After restoring PSModulePath to its original value, the module should no longer be found. This is important, because the next goal is to install the module such that it can be loaded without modifying PSModulePath.

Publishing Module Locally

Installing modules can only be done from a repository, so let’s publish the module to the local repository: $ModulePackage = «${RepoPath}/${ModuleName}.${ModuleVersion}.nupkg» Remove-Item -Path $ModulePackage -ErrorAction ‘SilentlyContinue’ Publish-Module -Path $ModulePath -Repository ‘local’

Cleaning Up

To restore the system to its original state, uninstall the test module, unregister the ‘local’ repository, and delete the “~/psrepo” and “~/modules” directories: Uninstall-Module -Name $ModuleName Unregister-PSRepository -Name ‘local’ Remove-Item «~/psrepo» -Force -Recurse -ErrorAction ‘SilentlyContinue’ Remove-Item «~/modules» -Force -Recurse -ErrorAction ‘SilentlyContinue’ Otherwise, keep the ‘local’ repository in place and start using it instead of a local NuGet server, or a repository hosted on a network share.

Closing Thoughts

If you found this blog post useful, please share it! We welcome your comments and would like to learn more about your experience with PowerShell module repositories. If you had a magic wand, what would you change about PowerShell module distribution? We want to know!