1

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:

  • Get-PSRepository returns NOTHING. Not even PSGallery

  • When I try to register a repository (using either Register-PSRepository or Register-PackageSource) it throws an error: PackageManagement\Register-PackageSource : The property 'Values' cannot be found on this object. Verify that the property exists.

  • as part of my script, I am running these cmdlets to make sure all the required modules are there:

    $webclient=New-Object System.Net.WebClient; $webclient.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials; [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Install-PackageProvider -Name NuGet -RequiredVersion 2.8.5.208 -Force -Confirm:$false -Verbose; Install-Module PowerShellGet -RequiredVersion 2.2.4 -SkipPublisherCheck -Verbose -Force;

  • Another error that comes up is this:

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

Mickey Cohen
  • 997
  • 7
  • 23

1 Answers1

1

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.

Kevin Lu-MSFT
  • 20,786
  • 3
  • 19
  • 28
  • I am doing that, but the problem is with registering the repo. See my original post, the errors are with the earlier phases, Get-PSRepository, Register-PSRepository, Register-PackageSource – Mickey Cohen Aug 01 '20 at 18:58
  • You could try to run the script: `Register-PSRepository -Default` . Then you could check if the PSGallery could be recovered. Please refer to this blog:https://spaghettidba.com/2017/12/19/recovering-the-psgallery-repository-behind-a-corporate-proxy/ – Kevin Lu-MSFT Aug 03 '20 at 09:03
  • If the PSGallery still couldn't find, this issue could relate with teh PowerShellGet Version. Check this ticket: https://stackoverflow.com/a/62456701/13464420 . You could try to upgrade the PowershellGet to 2.2.4 or 2.2.4.1 – Kevin Lu-MSFT Aug 06 '20 at 06:54