Mickey Cohen
Mickey Cohen

Reputation: 1277

Issues with PowerShell Repositories on Azure DevOps Agents

Sorry for the very long post, but I am really stuck and hope someone can help. I've been back and forth so many times and I am hitting many issues with everything that has to do with PowerShell repositories and Azure DevOps agents.

The end goal is to have the latest versions of some PowerShell modules installed as part of a pipeline.

I write various PowerShell modules, package them as NuGets and push them to different repositories (Azure DevOps artifacts, SonaType Nexus OSS)

I then need these modules installed as part of other pipelines. As there is no built-in way in Azure DevOps to handle PowerShell repositories and import modules, I wrote a script that takes the repository location, name and credentials as parameters, verifies it is registered and installs the module. When I run this script on ANY machine, it works perfectly When this script is a PowerShell task on any pipeline - it has various failures, always with cmdlets from PackageManagement

I thought this is because the agent is running it with -NoProfile, but it works for me when I run the script exactly how the agent runs it - "powershell.exe -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'C:.....'"

I also tried running a cmd task and calling PowerShell to run the script but had the exact same results

The problems I am hitting are:

PackageManagement\Get-PackageSource : Unable to find repository 'PSGallery'. Use Get-PSRepository to see all available repositories.

I spent hours on this. What is the correct way to install PS modules from a 3rd party repository (NuGet-based)

Thanks

Upvotes: 1

Views: 1724

Answers (1)

Kevin Lu-MSFT
Kevin Lu-MSFT

Reputation: 35119

I noticed that you have push your nuget package to Azure Artifacts.

You could add Powershell tasks to the pipeline and run the following scripts:

Register-PSRepository:

 $patToken = "PAT" | ConvertTo-SecureString -AsPlainText -Force
    
 $credsAzureDevopsServices = New-Object System.Management.Automation.PSCredential("email address", $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

Then you could register successfully.

enter image description here

enter image description here

Note: I suggest that you could create a Project Scope feed. Or you may get some issues.

Then you could run the following scripts to install the module.

Find-Module -Repository PowershellAzureDevopsServices

Install-Module -Name Get-Hello -Repository PowershellAzureDevopsServices

For more detailed information, you could refer to this Guidance Document.

Upvotes: 1

Related Questions