Search code examples
powershellazure-devopsazure-pipelinesazure-artifacts

How to install PowerShell module pushed to Azure Artifacts in Pipelines?


Following this article I set up Pipelines to push a PowerShell module to my Artifacts feed.

I can install the module on my local machine, but I was wondering how I can do the same in Pipelines? It seems adding the NuGet source is an interactive process, so how can Pipelines add the Artifacts feed as a source?

The issue is that I don't want to have any user interaction in a CI environment.


Solution

  • If you are using self-hosted agent, you need to configure the folder module permission, self-hosted agent run the cmd via service account instead of personal account.

    If you are using hosted agent, add the task power shell and enter below script to install the module.

    $patToken = "$(pat)" | ConvertTo-SecureString -AsPlainText -Force
    
    $credsAzureDevopsServices = New-Object System.Management.Automation.PSCredential("xxx", $patToken)
    
    
    Register-PSRepository -Name "PowershellAzureDevopsServices" -SourceLocation "https://pkgs.dev.azure.com/{Org name}/{project name}/_packaging/{feed name}/nuget/v2" -PublishLocation "https://pkgs.dev.azure.com/{Org name}/{project name}/_packaging/{feed name}/nuget/v2" -InstallationPolicy Trusted -Credential $credsAzureDevopsServices
    
    Get-PSRepository
    
    Find-Module -Repository PowershellAzureDevopsServices -Credential $credsAzureDevopsServices
    
    
    Install-Module -Name Get-Hello -Repository PowershellAzureDevopsServices -Credential $credsAzureDevopsServices
    
    
    Get-Module -ListAvailable Get-Hello
    

    Result:

    enter image description here

    Update1

    We need to enter the code during the registration of the power supply enclosure repository, the method is Register-PSRepository

    This is a certification issue, If we change the authentication method, maybe we don’t need to enter the code.

    In addition, We could also install the module via the cmd Install-Module Get-Hello -Scope CurrentUser -Force