ChatGPT解决这个技术问题 Extra ChatGPT

Powershell: How do I install the Nuget provider for PowerShell on a unconnected machine so I can install a nuget package from the PS command line?

I'm trying to install pswindowsupdate.2.0.0.4.nupkg from the Powershell command line on a Win 7 computer not connected to the internet. I'm running PS 5.1.14409.1005. I obtained the nupkg from https://www.preview.powershellgallery.com/packages/PSWindowsUpdate/2.0.0.4

The PS command Install-Module -Name pswindowsupdate.2.0.0.4.nupkg -Repository {path to pswindowsupdate.2.0.0.4.nupkg} throws an error message:

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{my login}\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 PowerShell to install and import the NuGet provider now?

Saying 'yes', of course, fails to install the NuGet provider because I'm not connected to the internet.

I obtained nuget.exe (v4.7.0), stored it in 'C:\Program Files\PackageManagement\ProviderAssemblies' and added 'C:\Program Files\PackageManagement\ProviderAssemblies' to my path statement. However, executing Install-Module -Name pswindowsupdate.2.0.0.4.nupkg -Repository {path to pswindowsupdate.2.0.0.4.nupkg} still fails.

What exactly is a NuGet provider? Is it just nuget.exe? How do I obtain and install the Nuget provider (v2.8.5.201 or greater) for PowerShell so I can install this nuget package from the PowerShell command line?

Note: Visual Studio is not in any way involved with my question


L
Luis Gouveia

Although I've tried all the previous answers, only the following one worked out:

1 - Open Powershell (as Admin)

2 - Run:

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

3 - Run:

Install-PackageProvider -Name NuGet

The author is Niels Weistra: Microsoft Forum


For some reason Microsoft have not updated that repository to a better SSL version hence I was getting errors before using command 2 to allow downgraded TLS versions. Thanks for the tip!
Solved my issue where I had an internet connection but install-package still complained about a connection issue
I think this should be the solution.
If FIPS is enabled on the system then also error is thrown
this does still need an internet connection, not sure why it was accepted as an answer
v
vineel

I accepted trebleCode's answer, but I wanted to provide a bit more detail regarding the steps I took to install the nupkg of interest pswindowsupdate.2.0.0.4.nupkg on my unconnected Win 7 machine by way of following trebleCode's answer.

First: after digging around a bit, I think I found the MS docs that trebleCode refers to:

Bootstrap the NuGet provider and NuGet.exe

Install-PackageProvider

To continue, as trebleCode stated, I did the following

Install NuGet provider on my connected machine

On a connected machine (Win 10 machine), from the PS command line, I ran Install-PackageProvider -Name NuGet -RequiredVersion 2.8.5.208 -Force. The Nuget software was obtained from the 'Net and installed on my local connected machine.

After the install I found the NuGet provider software at C:\Program Files\PackageManagement\ProviderAssemblies (Note: the folder name \ProviderAssemblies as opposed to \ReferenceAssemblies was the one minor difference relative to trebleCode's answer.

The provider software is in a folder structure like this:

C:\Program Files\PackageManagement\ProviderAssemblies
   \NuGet
      \2.8.5.208
         \Microsoft.PackageManagement.NuGetProvider.dll
     

Install NuGet provider on my unconnected machine

I copied the \NuGet folder (and all its children) from the connected machine onto a thumb drive and copied it to C:\Program Files\PackageManagement\ProviderAssemblies on my unconnected (Win 7) machine

I started PS (v5) on my unconnected (Win 7) machine and ran Import-PackageProvider -Name NuGet -RequiredVersion 2.8.5.208 to import the provider to the current PowerShell session.

I ran Get-PackageProvider -ListAvailable and saw this (NuGet appears where it was not present before):

Name                     Version          DynamicOptions                                                                                                                                                                      
----                     -------          --------------                                                                                                                                                                      
msi                      3.0.0.0          AdditionalArguments                                                                                                                                                                 
msu                      3.0.0.0                                                                                                                                                                                              
NuGet                    2.8.5.208        Destination, ExcludeVersion, Scope, SkipDependencies, Headers, FilterOnTag, Contains, AllowPrereleaseVersions, ConfigFile, SkipValidate                                             
PowerShellGet            1.0.0.1          PackageManagementProvider, Type, Scope, AllowClobber, SkipPublisherCheck, InstallUpdate, NoPathUpdate, Filter, Tag, Includes, DscResource, RoleCapability, Command, PublishLocati...
Programs                 3.0.0.0          IncludeWindowsInstaller, IncludeSystemComponent

Create local repository on my unconnected machine

On unconnected (Win 7) machine, I created a folder to serve as my PS repository (say, c:\users\foo\Documents\PSRepository)

I registered the repo: Register-PSRepository -Name fooPsRepository -SourceLocation c:\users\foo\Documents\PSRepository -InstallationPolicy Trusted

Install the NuGet package

I obtained and copied the nupkg pswindowsupdate.2.0.0.4.nupkg to c:\users\foo\Documents\PSRepository on my unconnected Win7 machine

I learned the name of the module by executing Find-Module -Repository fooPsRepository

Version    Name                                Repository           Description                                                                                                                      
-------    ----                                ----------           -----------                                                                                                                      
2.0.0.4    PSWindowsUpdate                     fooPsRepository      This module contain functions to manage Windows Update Client.

I installed the module by executing Install-Module -Name pswindowsupdate

I verified the module installed by executing Get-Command –module PSWindowsUpdate

CommandType     Name                                               Version    Source                                                                                                                 
-----------     ----                                               -------    ------                                                                                                                 
Alias           Download-WindowsUpdate                             2.0.0.4    PSWindowsUpdate                                                                                                        
Alias           Get-WUInstall                                      2.0.0.4    PSWindowsUpdate                                                                                                        
Alias           Get-WUList                                         2.0.0.4    PSWindowsUpdate                                                                                                        
Alias           Hide-WindowsUpdate                                 2.0.0.4    PSWindowsUpdate                                                                                                        
Alias           Install-WindowsUpdate                              2.0.0.4    PSWindowsUpdate                                                                                                        
Alias           Show-WindowsUpdate                                 2.0.0.4    PSWindowsUpdate                                                                                                        
Alias           UnHide-WindowsUpdate                               2.0.0.4    PSWindowsUpdate                                                                                                        
Alias           Uninstall-WindowsUpdate                            2.0.0.4    PSWindowsUpdate                                                                                                        
Cmdlet          Add-WUServiceManager                               2.0.0.4    PSWindowsUpdate                                                                                                        
Cmdlet          Enable-WURemoting                                  2.0.0.4    PSWindowsUpdate                                                                                                        
Cmdlet          Get-WindowsUpdate                                  2.0.0.4    PSWindowsUpdate                                                                                                        
Cmdlet          Get-WUApiVersion                                   2.0.0.4    PSWindowsUpdate                                                                                                        
Cmdlet          Get-WUHistory                                      2.0.0.4    PSWindowsUpdate                                                                                                        
Cmdlet          Get-WUInstallerStatus                              2.0.0.4    PSWindowsUpdate                                                                                                        
Cmdlet          Get-WUJob                                          2.0.0.4    PSWindowsUpdate                                                                                                        
Cmdlet          Get-WULastResults                                  2.0.0.4    PSWindowsUpdate                                                                                                        
Cmdlet          Get-WURebootStatus                                 2.0.0.4    PSWindowsUpdate                                                                                                        
Cmdlet          Get-WUServiceManager                               2.0.0.4    PSWindowsUpdate                                                                                                        
Cmdlet          Get-WUSettings                                     2.0.0.4    PSWindowsUpdate                                                                                                        
Cmdlet          Get-WUTest                                         2.0.0.4    PSWindowsUpdate                                                                                                        
Cmdlet          Invoke-WUJob                                       2.0.0.4    PSWindowsUpdate                                                                                                        
Cmdlet          Remove-WindowsUpdate                               2.0.0.4    PSWindowsUpdate                                                                                                        
Cmdlet          Remove-WUServiceManager                            2.0.0.4    PSWindowsUpdate                                                                                                        
Cmdlet          Set-WUSettings                                     2.0.0.4    PSWindowsUpdate                                                                                                        
Cmdlet          Update-WUModule                                    2.0.0.4    PSWindowsUpdate 

I think I'm good to go


@OP, nice details, this will surely get you some reputation ups here. I'm sure I'll be back looking at it one day too! lol. This seems like a good opportunity to create a couple scripts to automate this tedious process. I'll have to play with this on some VM's
Install-PackageProvider is downloading onegetcdn.azureedge.net/providers/… (via go.microsoft.com/fwlink/?LinkID=627338&clcid=0x409) and eventually getting to onegetcdn.azureedge.net/providers/… - in case you needed an alternate way to do this.
Hi, I am experiencing the same issue too. However my issue is slightly different, I don’t see the dynamicoption but still see the nuget 2.8.5.208 when I did a listavailable scriptAnyone face this issue before?
And they say installing Linux software is complicated... LOL.
This is a fantastic explanation @william-charlton. This completes the accepted answer very nicely. I succeeded in my offline package install. Thanks!
t
trebleCode

MSDocs state this for your scenario:

In order to execute the first time, PackageManagement requires an internet connection to download the Nuget package provider. However, if your computer does not have an internet connection and you need to use the Nuget or PowerShellGet provider, you can download them on another computer and copy them to your target computer. Use the following steps to do this: Run Install-PackageProvider -Name NuGet -RequiredVersion 2.8.5.201 -Force to install the provider from a computer with an internet connection. After the install, you can find the provider installed in $env:ProgramFiles\PackageManagement\ReferenceAssemblies\\\\\\ or $env:LOCALAPPDATA\PackageManagement\ProviderAssemblies\\\\\\. Place the folder, which in this case is the Nuget folder, in the corresponding location on your target computer. If your target computer is a Nano server, you need to run Install-PackageProvider from Nano Server to download the correct Nuget binaries. Restart PowerShell to auto-load the package provider. Alternatively, run Get-PackageProvider -ListAvailable to list all the package providers available on the computer. Then use Import-PackageProvider -Name NuGet -RequiredVersion 2.8.5.201 to import the provider to the current Windows PowerShell session.


I am facing the same issue and followed your steps but for step 4, when I do Get-PackageProvider -ListAvailable, nuget still doesn't show up. C:\Program Files\PackageManagement\ProviderAssemblies\nuget\2.8.5.201 exists but can't seem to use Nuget at all..
How can this be the solution when the Install-PackageProvider has no input for source location?
Sorry I don't know, I don't use NuGet with PowerShell in my role now and haven't touched NuGet in a couple of years. @WilliamCharlton posted a great answer below with more technical details. Scroll and best of luck!
The -RequiredVersion or -Force aren't necessary. As of this comment there's a newer version of 2.8.5.208 available.
Running the line [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 as mentioned below may be required if you get an error installing NuGet.
m
mellifluous

Try this:

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

Install-PackageProvider NuGet -Force

Set-PSRepository PSGallery -InstallationPolicy Trusted

P
Parker Lindey

The provider is bundled with PowerShell>=6.0.

If all you need is a way to install a package from a file, just grab the .msi installer for the latest version from the github releases page, copy it over to the machine, install it and use it.


Perhaps modify your answer to provide an example?
Y
Yves Martin

Here is the script I use in a Dockerfile based on windows/servercore to achieve complete PowerShellGallery setup through Artifactory mirrors (also for onegetcdn.azureedge.net)

ARG ONEGET_NUGET_PROVIDER="Microsoft.PackageManagement.NuGetProvider-2.8.5.208.dll"
ARG ONEGET_PROVIDERS="https://artifactory/artifactory/generic-azureedge-onegetcdn/providers/"
 
RUN $ProviderPath = 'C:/Program Files/PackageManagement/ProviderAssemblies/'; `
    New-Item -ItemType "directory" -Path $ProviderPath -Force; `
    Invoke-WebRequest -Uri "${Env:ONEGET_PROVIDERS}${Env:ONEGET_NUGET_PROVIDER}" -OutFile "${ProviderPath}${Env:ONEGET_NUGET_PROVIDER}"; `
    Register-PSRepository -Name "artifactory-powershellgallery-remote" -SourceLocation "https://artifactory/artifactory/api/nuget/powershellgallery-remote"; `
    Unregister-PSRepository -Name PSGallery;

Location and dll version are visible at https://onegetcdn.azureedge.net/providers/providers.masterList.feed.swidtag


It's a potential solution. I realized that my server can't connect to onegetcdn.azureedge.net.
E
Everton Barciela

None of the options in this thread worked for me. I'm using PowerShell Core 7.1.5. What worked for me was to remove Windows Powershell Modules from $env:PSModulePath. Essentially, check your environment variables and look for any path that has "WindowsPowerShell" and remove it.


a
adamency

In order to install the Nuget Package Manager non-interactively, simply use the -Force flag which bypasses prompting:

Install-PackageProvider NuGet -Force

You do not have to use the trick about the security protocol, at least for Windows Powershell (5.1).