33

I am having a difficult time installing/updating my powershell modules. I noticed this when I tried installing DBA Tools moudle. Reference links are https://dbatools.io/download/ and https://github.com/sqlcollaborative/dbatools.

It's a corporate PC. But I know that I have installed other modules before in the past. Does anyone have any idea what's going on?


PS (Admin)>

Install-Module DBATools

  • NOTE: The Install-Module command pauses for many minutes before the command returns a warning message.

WARNING: Unable to resolve package source 'https://www.powershellgallery.com/api/v2'. ERROR: "PackageManagement\Install-Package : No match was found for the specified search criteria and module name 'PowerShellGet'".

Update-Module PowerShellGet

ERROR: "Update-Module : Module 'PowerShellGet' was not installed by using Install-Module, so it cannot be updated.".

Update-Module PowerShellGet -Force

ERROR: "Update-Module : Module 'PowerShellGet' was not installed by using Install-Module, so it cannot be updated.".

Find-Module dbatools

  • NOTE: The Find-Module command pauses for many minutes before the command returns an error message.

ERROR: "No match was found for the specified search criteria and module name 'dbatools'. Try Get-PSRepository to see all available registered module repositories."

Get-PSRepository | fl *

Name : PSGallery

SourceLocation : https://www.powershellgallery.com/api/v2

Trusted : False

Registered : True

InstallationPolicy : Untrusted

PackageManagementProvider : NuGet

PublishLocation : https://www.powershellgallery.com/api/v2/package/

ScriptSourceLocation : https://www.powershellgallery.com/api/v2/items/psscript

ScriptPublishLocation : https://www.powershellgallery.com/api/v2/package/

ProviderOptions : {}

Get-Module PackageManagement -ListAvailable

Directory: C:\Program Files\WindowsPowerShell\Modules

ModuleType Version Name ExportedCommands

Binary 1.0.0.1 PackageManagement {Find-Package, Get-Package, Get-PackageProvider, Get-Packa...

Binary 1.0.0.1 PackageManagement {Find-Package, Get-Package, Get-PackageProvider, Get-Packa...

Get-Module -ListAvailable |
 Where-Object ModuleBase -like $env:ProgramFiles\WindowsPowerShell\Modules\* |
 Sort-Object -Property Name, Version -Descending |
 Get-Unique -PipelineVariable Module |
 ForEach-Object {
     if (-not(Test-Path -Path "$($_.ModuleBase)\PSGetModuleInfo.xml")) {
         Find-Module -Name $_.Name -OutVariable Repo -ErrorAction SilentlyContinue |
         Compare-Object -ReferenceObject $_ -Property Name, Version |
         Where-Object SideIndicator -eq '=>' |
         Select-Object -Property Name,
                                 Version,
                                 @{label='Repository';expression={$Repo.Repository}},
                                 @{label='InstalledVersion';expression={$Module.Version}}
     }

 }         

WARNING: Unable to resolve package source 'https://www.powershellgallery.com/api/v2'. WARNING: Unable to resolve package source 'https://www.powershellgallery.com/api/v2'. WARNING: Unable to resolve package source 'https://www.powershellgallery.com/api/v2'. WARNING: Unable to resolve package source 'https://www.powershellgallery.com/api/v2'. WARNING: Unable to resolve package source 'https://www.powershellgallery.com/api/v2'.

$webclient=New-Object System.Net.WebClient
$webclient.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
[Net.ServicePointManager]::SecurityProtocol = "tls12"
Find-Module dbatools

WARNING: Unable to resolve package source 'https://www.powershellgallery.com/api/v2'.

PackageManagement\Find-Package : No match was found for the specified search criteria and module name 'dbatools'. Try Get-PSRepository to see all available registered module repositories.

Invoke-WebRequest https://www.powershellgallery.com/api/v2

Invoke-WebRequest : The underlying connection was closed: The connection was closed unexpectedly.


Some references I tried

windows 10 - Powershell won't install almost any module using install-module - Stack Overflow Powershell won't install almost any module using install-module

There's a script for that: Install-Module - unable to resolve package source 'https //www.powershellgallery.com/api/v2/' https://vanbrenk.blogspot.com/2017/09/install-module-unable-to-resolve.html

Update Manually Installed PowerShell Modules from the PowerShell Gallery – Mike F Robbins https://mikefrobbins.com/2016/06/09/update-manually-installed-powershell-modules-from-the-powershell-gallery/

Update-Module : Module 'PowershellGet' was not installed by using Install-Module, so it cannot be updated. - Evotec https://evotec.xyz/update-module-module-powershellget-was-not-installed-by-using-install-module-so-it-cannot-be-updated/

SherlockSpreadsheets
  • 2,062
  • 3
  • 27
  • 47
  • 1
    Does `Find-Module dbatools` work? – Mathias R. Jessen Aug 12 '20 at 22:39
  • Find-Module dbatools response is `No match was found for the specified search criteria and module name 'dbatools'. Try Get-PSRepository to see all available registered module repositories.` I think the repo is registered. – SherlockSpreadsheets Aug 12 '20 at 23:30
  • What's the output of `Get-PSRepository | fl *` – Doug Maurer Aug 13 '20 at 01:04
  • Get-PSRepository | fl * output is `Name : PSGallery SourceLocation : https://www.powershellgallery.com/api/v2 Trusted : False Registered : True InstallationPolicy : Untrusted PackageManagementProvider : NuGet PublishLocation : https://www.powershellgallery.com/api/v2/package/ ScriptSourceLocation : https://www.powershellgallery.com/api/v2/items/psscript ScriptPublishLocation : https://www.powershellgallery.com/api/v2/package/ ProviderOptions : {}` – SherlockSpreadsheets Aug 13 '20 at 02:37
  • 1
    If this is a home system, this would be surprising. If this is a corporate/enterprise-managed machine, then they could be blocking you. I've seen blocking this in several customer environments. – postanote Aug 13 '20 at 05:50
  • 1
    It's a corporate PC. I've been using PS for a couple years at my corporation, and I have installed other modules before. What else can I check to get this resolved? – SherlockSpreadsheets Aug 13 '20 at 12:53
  • 1
    Do you get a valid response if you just send an HTTP request to the gallery directly? ie. `Invoke-WebRequest https://www.powershellgallery.com/api/v2` – Mathias R. Jessen Aug 19 '20 at 15:54
  • @MathiasR.Jessen - Updated the posting with the results. The underlying connection was closed. – SherlockSpreadsheets Aug 19 '20 at 16:32
  • 2
    It could be a TLS issue. Try forcing TLS1.2 by running the following command first: `[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12` – JonPayne Oct 27 '20 at 09:00

7 Answers7

41

I ran into the same error installing different module. My fix was specifying TLS1.2 for the .net Security protocol.

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
ggorlen
  • 44,755
  • 7
  • 76
  • 106
Stephen
  • 511
  • 4
  • 3
  • Worked great when I RDP'd in. Didn't work in PowerShell remoting (`Enter-PSSession`). – Eric Eskildsen Jan 16 '23 at 10:39
  • This is the correct answer. I was trying to use TLSv1.3 and was getting the "cant find package" error. Changed to using TLSv1.2 and it works. Kinda crazy to me that the newer TLS version doesn't work. – Rex Linder Jun 12 '23 at 15:23
23

Try running Register-PSRepository -Default

aditya
  • 331
  • 1
  • 4
  • 1
    This worked for me on a Win 2016 Standard with PSVersion 5.1.14393.2969 box, but only after I issued the TLS config setting mentioned in the answer by @Stephen – Justin Feb 20 '21 at 00:21
  • 1
    `PackageManagement\Register-PackageSource : Module Repository 'PSGallery' exists. At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:4211 char:17 + ... $null = PackageManagement\Register-PackageSource @PSBoundParamete .` – Amit Naidu Jun 11 '21 at 00:53
12

Thanks to Stephen, rouxquasar it worked with below order for me Windows 2016 Datacenter, KB4598243, Execution policy was set properly so didn't have to deal with that.

  1. Enable TLS 1.2:

    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

  2. Register the default PS Gallery Repo (may check Get-PSRepository | fl* just incase)

    Register-PSRepository -Default

  3. Install-Module dbatools (check Find-Module before to validate)

Use -Force switch if an older version of dbatools exists.

Rafs
  • 614
  • 8
  • 19
Vinay
  • 954
  • 8
  • 13
7

Re-registering PS default repository as an Administrator fixed for me:

  [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  Unregister-PSRepository -Name PSGallery
  Register-PSRepository -Default
  Find-Module dbatools
1

I have tried register-psrepository and it executed with out any error but after when I try get-psrepository its was still giving same error message "WARNING: Unable to find module repositories.", I tried different option below steps worked for me:

  1. register ps repository with a different name and use the same psgallery api for source location. Register-PSRepository PSGallery1 -SourceLocation "https://www.powershellgallery.com/api/v2" -InstallationPolicy Trusted

  2. set the repository Set-PSRepository PSGallery1

  3. now when I check the repository it worked like charm PSGAllery1 is the Name I have used

G_RAJA
  • 31
  • 3
0

Enabled Group Policy to allow scripts and set execution policy to bypass. I am now able to install the module. However, I must run the install with Scope current user and am still unable to install with As Admin powershell.

Module Install

Install-Module DBATools -Scope CurrentUser

Group Policy (Fix)

#*****************
FIX ...
#*****************

## PS (As Admin)
gpedit.msc
# Navigate to: Computer Configuration > Administrative Templates > Windows Components > Windows PowerShell. Change the “Turn on Script Execution”
# Turn on Script Execution > Enabled, Policy "Allow all scripts"

## PS (As Admin)
Set-ItemProperty -Path HKLM:\Software\Policies\Microsoft\Windows\PowerShell -Name ExecutionPolicy -Value ByPass


<# 
#*****************
REFERENCE ...
#*****************
 Windows PowerShell - the setting is overridden by a policy defined at a more specific scope
 https://vladtalkstech.com/2015/03/windows-powershell-updated-execution-policy-successfully-setting-overridden-policy-defined-specific-scope.html

 Change the PowerShell MachinePolicy Execution Policy in WS 2012R2
 https://vladtalkstech.com/2014/03/change-powershell-machinepolicy-execution-policy-windows-server-2012r2.html
#>


#*****************
CAUSE ...
#*****************

Get-ExecutionPolicy –List

Set-ExecutionPolicy -Scope MachinePolicy Unrestricted

## However, you might have an error saying it can only be done via Group Policy.
## “Set-ExecutionPolicy : Cannot set execution policy. Execution policies at the MachinePolicy or UserPolicy scopes mustbe set through Group Policy
SherlockSpreadsheets
  • 2,062
  • 3
  • 27
  • 47
0

Try running the powerShell or the WindowsTerminal as administrator, and then re-entering your command before trying any complicated solution, helps most of the time.

Yehdhih ANNA
  • 1,222
  • 1
  • 11
  • 22