Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to find repository on Update-Module

Tags:

powershell

I'm using Windows 10 and Powershell 5.1

Get-PSRepository has result :

PSGallery Untrusted https://www.powershellgallery.com/api/v2

whereas Update-Module returns error

PackageManagement\Install-Package : Unable to find repository 'https://www.powershellgallery.com/api/v2/'. Use Get-PSRepository to see all available repositories. At C:\Program Files\WindowsPowerShell\Modules\powershellget\2.0.1\PSModule.psm1:13000 char:20 + ... $sid = PackageManagement\Install-Package @PSBoundParameters + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Microsoft.Power....InstallPackage:InstallPackage) [Install-Package], Ex ception + FullyQualifiedErrorId : SourceNotFound,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage

Any idea of how to fix it?

like image 997
AfshinZavvar Avatar asked Nov 18 '18 07:11

AfshinZavvar


People also ask

How do I register my PSRepository?

The Register-PSRepository cmdlet registers the default repository for PowerShell modules. After a repository is registered, you can reference it from the Find-Module , Install-Module , and Publish-Module cmdlets. The registered repository becomes the default repository in Find-Module and Install-Module .

How do I remove a PowerShell module?

The Remove-Module cmdlet removes the members of a module, such as cmdlets and functions, from the current session. If the module includes an assembly (. dll), all members that are implemented by the assembly are removed, but the assembly is not unloaded.


2 Answers

TL;DR

It looks like the URL for the PSGallery repository registered in PowerShell used to point to https://www.powershellgallery.com/api/v2/ but it was changed to https://www.powershellgallery.com/api/v2 at some point (note the missing forward slash at the end).

λ  Get-PSRepository  Name                      InstallationPolicy   SourceLocation ----                      ------------------   -------------- PSGallery                 Untrusted            https://www.powershellgallery.com/api/v2 

Any modules installed from the old URL are now failing to update. Reinstalling them from PowerShell gallery will update the repository URL, allowing the modules to be updated normally going forward. You can use the following command to reinstall all modules pointing to the old URL:

Get-InstalledModule ` | ? { $_.Repository -eq 'https://www.powershellgallery.com/api/v2/' } ` | % { Install-Package -Name $_.Name -Source PSGallery -Force -AcceptLicense } 

The full run down

I have run into this incredibly annoying issue myself. From the error message we can see a couple of things:

PackageManagement\Install-Package : Unable to find repository 'https://www.powershellgallery.com/api/v2/'

  1. PowerShellGet\Update-Module ultimately passes the buck to PackageManagement\Install-Package
  2. It is looking for a repository at 'https://www.powershellgallery.com/api/v2/'

Running Get-PSRepository on my machine yields:

Name                      InstallationPolicy   SourceLocation ----                      ------------------   -------------- PSGallery                 Trusted              https://www.powershellgallery.com/api/v2 

So it looks like the repository is there, except, maybe it isn't. Take note of the trailing forward slash. Could it be that Install-Package is looking for a repository with a SourceLocation that exactly matches that string? Let's try changing the SourceLocation for PSGallery:

Set-PSRepository -Name PSGallery -SourceLocation https://www.powershellgallery.com/api/v2/ -InstallationPolicy Trusted 

PackageManagement\Set-PackageSource : The PSGallery repository has pre-defined locations. The 'Location, NewLocation or SourceLocation' parameter is not allowed, try again after removing the 'Location, NewLocation or SourceLocation' parameter. At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.0.4\PSModule.psm1:11768 char:17 + ... $null = PackageManagement\Set-PackageSource @PSBoundParameters + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (https://www.pow...ery.com/api/v2/:String) [Set-PackageSource], Exception + FullyQualifiedErrorId : ParameterIsNotAllowedWithPSGallery,Add-PackageSource,Microsoft.PowerShell.PackageManagement.Cmdlets.SetPackageSource

Well, that didn't work. Looks like the PSGallery repository is protected for your safety.

Let's trying adding a different repository and updating a module:

Register-PSRepository -Name PSGallery1 -SourceLocation https://www.powershellgallery.com/api/v2/ -InstallationPolicy Trusted Update-Module -Name pester -Force 

Look, no error. It works!

Here is the interesting thing, if I pull up a list of installed modules I find a mix of repositories:

Get-InstalledModule | Select Name, Repository | FT -AutoSize  Name                         Repository ----                         ---------- 7Zip4Powershell              PSGallery  AWSPowerShell                PSGallery  cChoco                       PSGallery1 dbatools                     PSGallery  DLMAutomation                PSGallery1 InvokeBuild                  PSGallery1 Microsoft.PowerShell.Archive PSGallery1 PackageManagement            PSGallery  Pester                       PSGallery1 posh-git                     PSGallery1 powershell-yaml              PSGallery1 PowerShellGet                PSGallery  PowerUpSQL                   PSGallery1 psake                        PSGallery1 PsHosts                      PSGallery1 psTrustedHosts               PSGallery1 ReverseDSC                   PSGallery1 SeeShell                     PSGallery1 SqlServer                    PSGallery1 TunableSSLValidator          PSGallery1 xSmbShare                    PSGallery1 xWebAdministration           PSGallery1 

Look at all those modules installed form PSGallery1 which is associated with https://www.powershellgallery.com/api/v2/! Prior to just now, there has never been a repository on my machine called PSGallery1; every module I have ever installed has been from PSGallery. My guess is that the PSGallery repository used to point to https://www.powershellgallery.com/api/v2/ and at some point, intentionally or not, it was changed to https://www.powershellgallery.com/avp/v2; breaking Update-Module for any modules installed from the previous URL. I suspect that if I reinstall the modules using Install-Package from the updated PSGallery repository everything will resolve itself and I can remove the PSGallery1 repository.

Let's update all the modules that were deployed from the old URL (PSGallery1):

Get-InstalledModule ` | ? { $_.Repository -eq 'PSGallery1' } ` | % { Install-Package -Name $_.Name -Source PSGallery -Force -AcceptLicense } 

Running Get-InstalledModule again yields:

Name                         Repository ----                         ---------- 7Zip4Powershell              PSGallery AWSPowerShell                PSGallery cChoco                       PSGallery dbatools                     PSGallery DLMAutomation                PSGallery InvokeBuild                  PSGallery Microsoft.PowerShell.Archive PSGallery PackageManagement            PSGallery Pester                       PSGallery posh-git                     PSGallery powershell-yaml              PSGallery PowerShellGet                PSGallery PowerUpSQL                   PSGallery psake                        PSGallery PsHosts                      PSGallery psTrustedHosts               PSGallery ReverseDSC                   PSGallery SeeShell                     PSGallery SqlServer                    PSGallery TunableSSLValidator          PSGallery xSmbShare                    PSGallery xWebAdministration           PSGallery 

Great! Now let's try removing the PSGallery1 repository and updating a module:

Unregister-PSRepository PSGallery1 Update-Module -Name pester -Force 

Success! The module updated without error.

I'm not sure what is broken here, the URL for the PSGallery repository or Install-Package, but reinstalling all modules that were installed from the old URL seems to fix everything.

like image 176
Jason Boyd Avatar answered Sep 23 '22 12:09

Jason Boyd


After trying all kinds of things, forcing a reinstall of the NuGet package provider seems to have cleared up the issue with Update-Module for me.

Execute this in an elevated PowerShell session:

Install-PackageProvider Nuget –Force 

For reference, I was here when I had my best success: https://docs.microsoft.com/en-us/powershell/scripting/gallery/installing-psget

like image 24
Doug Baer Avatar answered Sep 25 '22 12:09

Doug Baer