Skip to content

Instantly share code, notes, and snippets.

@SteveH3032
Last active September 12, 2019 13:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SteveH3032/16a0343858bdcd0ae321f0f599539af4 to your computer and use it in GitHub Desktop.
Save SteveH3032/16a0343858bdcd0ae321f0f599539af4 to your computer and use it in GitHub Desktop.
Nov 2018 - PowerShell script to download all NuGet packages
$indexClient = new-object system.net.webclient
$jsonIndex=$indexClient.DownloadString("https://api.nuget.org/v3/catalog0/index.json") | ConvertFrom-Json
for ($h=0; $h -lt $jsonIndex.items.Length; $h++) {
$web_client = new-object system.net.webclient
$jsonObj=$web_client.DownloadString($jsonIndex.items[$h].'@id') | ConvertFrom-Json
for ($i=0; $i -lt $jsonObj.items.Length; $i++) {
$package = $jsonObj.items[$i]."nuget:id" + "/" + $jsonObj.items[$i]."nuget:version"
$web_client.DownloadFile("https://www.nuget.org/api/v2/package/" + $package, ("C:\Nuget\" + $jsonObj.items[$i]."nuget:id" + "." + $jsonObj.items[$i]."nuget:version" + ".nupkg"))
"Processing: " + ($h + 1) + " of " + $jsonIndex.count + " ---- " + ($i + 1) + " of " + $jsonObj.count + " - " + $jsonObj.items[$i]."nuget:id" + "." + $jsonObj.items[$i]."nuget:version"
}
}
@JerkerPihl
Copy link

$webClient should be $web_client

@SteveH3032
Copy link
Author

SteveH3032 commented Nov 25, 2018

Thanks JerkerPihl. Only after running this did I notice the statement "Because the catalog is not used by the official NuGet client, not all package sources implement the catalog." on the site https://docs.microsoft.com/en-us/nuget/api/catalog-resource.

So I offer the following:

Step 1 - Open Visual Studio 2017
Step 2 - Open the NuGet Package Manager Console
a. Click the Tools menu
b. Click NuGet Package Manager
c. Click Package Manager Console
Step 3 - Enter the following command:
Get-Package -ListAvailable -PageSize 122831 | Export-Csv -Path "C:\NugetList\NuGet_All.csv"
Step 4 - Run the following
`
$path = "C:\NuGetList\NuGet_All.csv"
Import-Csv $path | Foreach-Object {
$url = "https://www.nuget.org/api/v2/package/$($_.id)/$($_.version)"
$output = "C:\Nuget$($.id).$($.version).nupkg"
(New-Object System.Net.WebClient).DownloadFile($url, $output)
}

@TwoXTwentyOne
Copy link

TwoXTwentyOne commented Mar 26, 2019

Running the script above in Powershell I get the following error:

$.id : The term '$.id' 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 E:\Nuget_All.ps1:4 char:29
+ $OUTPUT = "E:\Nuget$\Nuget$($.id).$($.version).nupkg"
+                             ~~~~
    + CategoryInfo          : ObjectNotFound: ($.id:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

$.version : The term '$.version' 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 E:\Nuget_All.ps1:4 char:37
+ $OUTPUT = "E:\Nuget$\Nuget$($.id).$($.version).nupkg"
+                                     ~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: ($.version:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment