I want to understand if it is possible to install/import a custom module and have its functions available for multiple Azure Powershell Tasks within the same Azure DevOps release pipeline stage.
Background
I have a custom powershell module (written by myself) that is packaged correctly into a nuget package by an Azure DevOps Build pipeline. This is stored in Azure Artifacts.
I reference this Azure Artifacts Powershell module in the Release pipeline and when run, the pipeline extracts correctly and I can see all the expected powershell scripts within the $Agent.ReleaseDirectory
In the DevOps Release pipeline Stage I have two tasks
- Task 1) An initialisation Azure Powershell task intended to load the module and make the module functions available to the session and therefore available to later Azure Powershell tasks.
- Task 2) A 'test' Azure Powershell task that calls a "Get-BuildNumber" powershell function that is part of the module.
The Problem
- If I call "Get-BuildNumber" from Task 1 (that loads the module) then I get back my build number correctly. A write-host command confirms the value is correct.
- If I call "Get-BuildNumber" from Task 2 (the one that tests the functions are available) then I get the response
The term 'Get-BuildNumber' is not recognized as the name of a cmdlet, function, script file, or operable program.
Analysis
- It appears that the imported module is scoped to a single azure powershell task but I'd rather not have to load the module for each task.
- I'm using the following commands in task 1 and the module itself 'dot sources' the script functions, making each function available in the session scope. On my local machine this all works correctly.
$currentDir = Split-Path -Parent $MyInvocation.MyCommand.Path
Import-Module (Join-Path $currentDir "modules\deploy") -Force
$env:PSUTILSPATH = (Join-Path $currentDir "modules")
The deploy.psm1 module file just contains the following
$functions = Get-ChildItem -Recurse $PSScriptRoot -Include *.ps1 | ? { $_ -notmatch ".Tests.ps1" }
foreach ($function in $functions)
{
. $function.FullName
}
- Should I use Install-Module rather than Import-Module?
- Maybe I need to import the module for each powershell task rather than attempting to load it once and reference many times?
Any help would be much appreciated.
As far as I understand your requirement is to import a custom module for only one time, so that the subsequent Azure PowerShell task(s) doesn't have to import the module ever again.
I have tested with several methods, while only the trick below works. The workaround is to copy the custom module into the path
C:\Program Files\WindowsPowerShell\Moduleswhich is one of the path of$PSModulePath.Get-Helloto my Azure Artifacts feed as a private PowerShell repository;CopyFiles@2to copy theGet-Help(Source alias) directory together with the required module files in it into the target pathC:\Program Files\WindowsPowerShell\Modules;According to this document, How to Write a PowerShell Script Module - PowerShell | Microsoft Learn
I tested to add the location of my custom modules into
$PSModulePath, but the modification was not updated in the downstream tasks. Besides, the suggested steps here didn't seem to work.