Skip to content

Instantly share code, notes, and snippets.

@bevand
Last active March 19, 2024 23:25
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save bevand/e5325035a31c281f4532e330005216cc to your computer and use it in GitHub Desktop.
Save bevand/e5325035a31c281f4532e330005216cc to your computer and use it in GitHub Desktop.
Powershell script to download all packages from a nuget feed
$destinationDirectory = "C:\LocalNuGetTest\"
$webClient = New-Object System.Net.WebClient
$webClient.Credentials = New-Object System.Net.NetworkCredential("USERNAME", "PASSWORD")
$feed =[xml]$webClient.DownloadString("https://hostednugetfeed.com/custom-feed/nuget/v2/Packages")
$records = $feed | select -ExpandProperty feed | select -ExpandProperty entry #| select -ExpandProperty content
for ($i=0; $i -lt $records.Length; $i++) {
$content = $records[$i] | select -ExpandProperty content
$properties = $records[$i] | select -ExpandProperty properties
$title = $records[$i].title.'#text'
$url = $content.src
$startOfQuery = $url.IndexOf("?id=") + 4
$fileName = $url.Substring($startOfQuery, $url.Length - $startOfQuery)
$fullPath = ($destinationDirectory + "" + $fileName)
$webClient.DownloadFile($content.src, ($destinationDirectory + "" + $title + "." + $properties.Version + ".nupkg"))
}
@daevski
Copy link

daevski commented Jul 3, 2020

Interesting! However this only downloads 100 first nupkgs when I use this URL: https://www.powershellgallery.com/api/v2/Packages
Is there a way to make the script crawl the entire repository?

Yes, @EricSP2, and for anyone else wondering, there are ref links which will give you the next batch. Something like this:

$r = Invoke-WebRequest -uri "https://example.com/nuget/api/v2/Packages"
$RequestContent = [xml] $r
$NextLink = $RequestContent.feed.link.href[1]

Invoke-WebRequest -Uri "$NextLink"

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