-
-
Save bevand/e5325035a31c281f4532e330005216cc to your computer and use it in GitHub Desktop.
$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")) | |
} |
not work but thanks
Nice work. I've created an updated version pointing at NuGet which does not require credentials.
https://gist.github.com/SteveH3032/16a0343858bdcd0ae321f0f599539af4
Thanks!
Excellent script Bevand!
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?
I created a modified version for MyGet, this script also pulls symbol packages (if available) and can work with multiple feeds
this is tested with powershell 6, should work with 5 if you register a v2 feed endpoint...
$Token = "xxxxxx-xxxxxx-xxxxxx-xxxx"
$WebClient = New-Object System.Net.WebClient
$Feeds = "a","b","c"
New-Item -Name "feeds" -Type 'directory' -Force
ForEach ($Feed in $Feeds) {
Register-PackageSource "${Feed}" -Location "https://stylelabs.myget.org/F/${Feed}/auth/${Token}/api/v3/index.json" -Trusted -ProviderName NuGet -Force
New-Item -Name ".\feeds${Feed}" -Type 'directory' -Force
Find-Package -AllowPrereleaseVersions -AllVersions -Source "${Feed}" | ForEach-Object {
$Url = "https://stylelabs.myget.org/F/${Feed}/auth/${Token}/api/v2/package/$($_.Name)/$($_.Version)"
$SymbolUrl = "https://stylelabs.myget.org/F/${Feed}/auth/${Token}/symbols/$($_.Name)/$($_.Version)"
$FileName = "$($_.Name).$($_.Version).nupkg"
$SymbolFileName = "$($_.Name).$($_.Version).symbols.nupkg"
Write-Output "Downloading $($_.Name) $($_.Version)"
$WebClient.DownloadFile($Url, ".\feeds\${Feed}\" + $FileName)
try {
$WebClient.DownloadFile($SymbolUrl, ".\feeds\${Feed}\" + $SymbolFileName)
} catch { Write-Warning "${SymbolFileName} not found" }
}
Unregister-PackageSource -Name "${Feed}" -Force
}
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"
THANK YOU!