Posted On June 14, 2020

PowerShell: Error Unable to find package provider ‘NuGet’ Resolved

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Error Unable to find package provider ‘NuGet’ Resolved
How to install module in Powow Shill

Current Version:

$moduleCommand='New-SSHSession'
$moduleName='Posh-SSH'
if(!(get-command $moduleCommand -ea Ignore)){
    write-host "Installing $moduleName..."
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    if(!(get-packageprovider nuget)){
        $null=Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -ErrorAction SilentlyContinue
        # Preempt this Nuget installation prompt for user-input
        # NuGet provider is required to continue
        # PowerShellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositories. The NuGet
        # provider must be available in 'C:\Program Files\PackageManagement\ProviderAssemblies' or
        # 'C:\Users\kdoan\AppData\Local\PackageManagement\ProviderAssemblies'. You can also install the NuGet provider by running
        # 'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force'. Do you want PowerShellGet to install and
        # import the NuGet provider now?
        # [Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"):  
    }        
    try{
        $null=Install-Module -Name $moduleName -Force -EA SilentlyContinue
    }catch{
        $null=Register-PSRepository -Default
        $null=Install-Module -Name $moduleName -Force -EA SilentlyContinue
        # Error caused by PSRepository being untrusted
        # PackageManagement\Install-Package : No match was found for the specified search criteria and module name 'Posh-SSH'. Try Get-PSRepository to see all available
        # registered module repositories.
        # At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:1772 char:21
        # + ...          $null = PackageManagement\Install-Package @PSBoundParameters
        # +                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        #     + CategoryInfo          : ObjectNotFound: (Microsoft.Power....InstallPackage:InstallPackage) [Install-Package], Exception
        #     + FullyQualifiedErrorId : NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage
        # The fix:
        # Set-executionPolicy bypass
        # Register-PSRepository -Default
    }
    refreshenv
    if(!(get-command $moduleCommand -ea Ignore)){
        write-warning "$moduleName is still NOT available on $env:computername"
        return $False
    }
}
# Michael Shoff has recently enforced TLS1.2
# Assuming that the error above is unrelated to Proxy and outbound firewall restrictions
# Here's the the solution: set PowerShell to default to TLS1.2 in current session as well as fixing it in the registry for next sessions
# Requirement: PowerShell session is running in the Administrator context

# Set module name
$moduleName='SqlServer'
# Install module if it's not already available  
if(!(Get-Module -ListAvailable -Name $moduleName -ea SilentlyContinue)){
    if(!('NuGet' -in (get-packageprovider).Name)){    
        try{
            Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -ErrorAction SilentlyContinue;
            }
        catch{
            Set-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord
            Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord
            [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
            Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -ErrorAction SilentlyContinue;
            }            
        }
    Install-Module -Name $moduleName -Force -Confirm:$false
    }
# Sample output:
#Name                           Version          Source           Summary
#----                           -------          ------           -------
#nuget                          2.8.5.208        https://onege... NuGet provider for the OneGet meta-package manager
Original gibberish to be avoided:
#PS C:\Windows\system32> Install-Module -Name WinSCP
#PackageManagement\Install-Package : No match was found for the specified search criteria and module name 'WinSCP'. Try
#Get-PSRepository to see all available registered module repositories.
#At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:1772 char:21
#+ ...          $null = PackageManagement\Install-Package @PSBoundParameters
#+                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#    + CategoryInfo          : ObjectNotFound: (Microsoft.Power....InstallPackage:InstallPackage) [Install-Package], Ex
#   ception
#    + FullyQualifiedErrorId : NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage

#Install-Module PackageManagement -Force -Repository PSGallery
#PackageManagement\Get-PackageSource : Unable to find repository 'PSGallery'. Use Get-PSRepository to see all available repositories.
#At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:4451 char:35
#+ ... ckageSources = PackageManagement\Get-PackageSource @PSBoundParameters
#+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#    + CategoryInfo          : ObjectNotFound: (Microsoft.Power...etPackageSource:GetPackageSource) [Get-PackageSource], Exception
#    + FullyQualifiedErrorId : SourceNotFound,Microsoft.PowerShell.PackageManagement.Cmdlets.GetPackageSource

#PS C:\Windows\system32> Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Verbose -Force
#VERBOSE: Using the provider 'Bootstrap' for searching packages.
#VERBOSE: Finding the package 'Bootstrap::FindPackage' 'NuGet','','2.8.5.201','''.
#WARNING: Unable to download from URI 'https://go.microsoft.com/fwlink/?LinkID=627338&clcid=0x409' to ''.
#VERBOSE: Cannot download link 'https://go.microsoft.com/fwlink/?LinkID=627338&clcid=0x409', retrying for '2' more times.
#VERBOSE: Cannot download link 'https://go.microsoft.com/fwlink/?LinkID=627338&clcid=0x409', retrying for '1' more times.
#VERBOSE: Cannot download link 'https://go.microsoft.com/fwlink/?LinkID=627338&clcid=0x409', retrying for '0' more times.
#WARNING: Unable to download the list of available providers. Check your internet connection.
#Install-PackageProvider : No match was found for the specified search criteria for the provider 'NuGet'. The package provider requires 'PackageManagement' and 'Provider' tags.
#Please check if the specified package has the tags.
#At line:1 char:1
#+ Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Verbos ...
#+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#    + CategoryInfo          : InvalidArgument: (Microsoft.Power...PackageProvider:InstallPackageProvider) [Install-PackageProvider], Exception
#    + FullyQualifiedErrorId : NoMatchFoundForProvider,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackageProvider

#The cause: this machine has applied protocol/cipher suite hardening
#The solution: configure the PS Session to use TLS 1.2

#PS C:\Windows\system32> [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
#PS C:\Windows\system32> Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Verbose -Force
#VERBOSE: Using the provider 'Bootstrap' for searching packages.
#VERBOSE: Finding the package 'Bootstrap::FindPackage' 'NuGet','','2.8.5.201','''.
#VERBOSE: Performing the operation "Install Package" on target "Package 'nuget' version '2.8.5.208' from
#'https://onegetcdn.azureedge.net/providers/nuget-2.8.5.208.package.swidtag'.".
#VERBOSE: Installing the package 'https://onegetcdn.azureedge.net/providers/nuget-2.8.5.208.package.swidtag'.
#VERBOSE: Installed the package 'nuget' to 'C:\Program Files\PackageManagement\ProviderAssemblies\nuget\2.8.5.208\Microsoft.PackageManagement.NuGetProvider.dll'.
#VERBOSE: Skipping previously processed provider 'NuGet'.
#
#Name                           Version          Source           Summary
#----                           -------          ------           -------
#nuget                          2.8.5.208        https://onege... NuGet provider for the OneGet meta-package manager
#VERBOSE: Importing the package provider NuGet
#VERBOSE: The provider 'NuGet' has already been imported. Trying to import it again.
#VERBOSE: Importing package provider 'NuGet'.
#VERBOSE: Imported provider 'C:\Program Files\PackageManagement\ProviderAssemblies\nuget\2.8.5.208\Microsoft.PackageManagement.NuGetProvider.dll' .
#WARNING: Unable to download from URI 'https://go.microsoft.com/fwlink/?LinkID=627338&clcid=0x409' to ''.
#WARNING: Unable to download the list of available providers. Check your internet connection.
#
#NuGet provider is required to continue
#PowerShellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositories. The NuGe
# provider must be available in 'C:\Program Files\PackageManagement\ProviderAssemblies' or
#'C:\Users\concu\AppData\Local\PackageManagement\ProviderAssemblies'. You can also install the NuGet provider b
# running 'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force'. Do you want PowerShellGet to install
#and import the NuGet provider now?
#[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): y
#WARNING: Unable to download from URI 'https://go.microsoft.com/fwlink/?LinkID=627338&clcid=0x409' to ''.
#WARNING: Unable to download the list of available providers. Check your internet connection.
#PackageManagement\Install-PackageProvider : No match was found for the specified search criteria for the provider
#'NuGet'. The package provider requires 'PackageManagement' and 'Provider' tags. Please check if the specified package
#has the tags.
#At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:7405 char:21
#+ ...     $null = PackageManagement\Install-PackageProvider -Name $script:N ...
#+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#    + CategoryInfo          : InvalidArgument: (Microsoft.Power...PackageProvider:InstallPackageProvider) [Install-Pac
#   kageProvider], Exception
#    + FullyQualifiedErrorId : NoMatchFoundForProvider,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackagePro
#   vider
#
#PackageManagement\Import-PackageProvider : No match was found for the specified search criteria and provider name
#'NuGet'. Try 'Get-PackageProvider -ListAvailable' to see if the provider exists on the system.
#At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:7411 char:21
#+ ...     $null = PackageManagement\Import-PackageProvider -Name $script:Nu ...
#+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#    + CategoryInfo          : InvalidData: (NuGet:String) [Import-PackageProvider], Exception
#    + FullyQualifiedErrorId : NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.ImportPackageProv
#   ider
#
#WARNING: Unable to download from URI 'https://go.microsoft.com/fwlink/?LinkID=627338&clcid=0x409' to ''.
#WARNING: Unable to download the list of available providers. Check your internet connection.
#PackageManagement\Get-PackageProvider : Unable to find package provider 'NuGet'. It may not be imported yet. Try
#'Get-PackageProvider -ListAvailable'.
#At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:7415 char:30
#+ ... tProvider = PackageManagement\Get-PackageProvider -Name $script:NuGet ...
#+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#    + CategoryInfo          : ObjectNotFound: (Microsoft.Power...PackageProvider:GetPackageProvider) [Get-PackageProvi
#   der], Exception
#    + FullyQualifiedErrorId : UnknownProviderFromActivatedList,Microsoft.PowerShell.PackageManagement.Cmdlets.GetPacka
#   geProvider
#
#Install-Module : NuGet provider is required to interact with NuGet-based repositories. Please ensure that '2.8.5.201'
#or newer version of NuGet provider is installed.
#At line:7 char:9
#+         Install-Module -Name SqlServer -Force -Confirm:$false
#+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#    + CategoryInfo          : InvalidOperation: (:) [Install-Module], InvalidOperationException
#    + FullyQualifiedErrorId : CouldNotInstallNuGetProvider,Install-Module
#
#Get-SqlDatabase : The term 'Get-SqlDatabase' is not recognized as the name of a cmdlet, function, script file, or
#operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
#again.
#At line:11 char:18
#+     $sqlInstance=Get-SqlDatabase -ServerInstance $sqlServer
#+                  ~~~~~~~~~~~~~~~
#    + CategoryInfo          : ObjectNotFound: (Get-SqlDatabase:String) [], CommandNotFoundException
#    + FullyQualifiedErrorId : CommandNotFoundException
#
#Restore-SqlDatabase : The term 'Restore-SqlDatabase' is not recognized as the name of a cmdlet, function, script file,
#or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and
#try again.
#At line:13 char:5
#+     Restore-SqlDatabase -ServerInstance $sqlInstance -Database "$OrgN ...
#+     ~~~~~~~~~~~~~~~~~~~
#    + CategoryInfo          : ObjectNotFound: (Restore-SqlDatabase:String) [], CommandNotFoundException
#    + FullyQualifiedErrorId : CommandNotFoundException

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

How to Add a Replica into an Existing Availability Group using PowerShell

Overview:0. Add secondary replica to the cluster by running some commands on the availability group…

IIS Mime Types

One of the features of IIS security is to enforce file access by its associated…

PowerShell: Windows 10 Cleanup

Update: there's a new script to cleanup windows here. function cleanWindows{ write-host 'Disabling Windows Media…