In a blink of an eye, I haven't written a blog for several years. I've come to the blog park to have a breeze. Recently, due to work needs, I work in the intranet. Fortunately, I only need to upload a *.nupkg package information to download it in a private nuget. Let's write a download script using PowerShell. What you need to pay attention to is the PowerShell suffix.ps1(The last number 1), as an example:
Download address
# Set the URL of the NuGet package list$packageName = "" $targetHttp = "/packages/" $targetUrl = "{0}{1}" -f $targetHttp, $packageName
Save the address
# Set the directory to save the downloaded package$outputDirectory = "D:\nuget_packages" if (-not (Test-Path $outputDirectory)) { New-Item -Path $outputDirectory -ItemType Directory }
Resolve the download version address
Define the package address that needs to be parsed when downloading
# Define download prefix$httpPrefix = "/api/v2/package/" # Download the content of the html file$htmlContent = Invoke-WebRequest -Uri $targetUrl -UseBasicParsing | Select-Object -ExpandProperty Content # Match tags$pattern = "<.*?>" $matches = [regex]::Matches($htmlContent, $pattern)
Get all a tags
foreach ($match in $matches) { $tag = $ # Get a tagif ($tag -like "<a href=*") { Write-Host $tag } }
Output result
<a href="#" rel="external nofollow" rel="external nofollow" class="showOnFocus" title="Skip To Content">
...
<a href="/packages//" rel="external nofollow" >
<a href="/packages//13.0.3" rel="external nofollow" rel="external nofollow" title="13.0.3">
...
<a href="/packages//3.5.8" rel="external nofollow" rel="external nofollow" title="3.5.8">
<a href="/stats/packages/?groupby=Version" rel="external nofollow" rel="external nofollow" title="Package Statistics">
...
<a href="/packages//13.0.3/ReportAbuse" rel="external nofollow" rel="external nofollow" title="Report the package as abusive">
<a href="/packages//13.0.3/ContactOwners" rel="external nofollow" rel="external nofollow" title="Ask the package owners a question">
...
Observe the results of the previous step and you can see that each version has a title, and the title content is a version
# Get a tag with titleif ($tag -like "*title=*") { Write-Host $tag }
Output result
<a href="#" rel="external nofollow" rel="external nofollow" class="showOnFocus" title="Skip To Content">
<a href="/packages//13.0.3" rel="external nofollow" rel="external nofollow" title="13.0.3">
...
<a href="/packages//3.5.8" rel="external nofollow" rel="external nofollow" title="3.5.8">
<a href="/stats/packages/?groupby=Version" rel="external nofollow" rel="external nofollow" title="Package Statistics">
<a href="/json" rel="external nofollow" data-track="outbound-project-url" title="Visit the project site to learn more about this package" >
...
<a href="/packages//13.0.3/ReportAbuse" rel="external nofollow" rel="external nofollow" title="Report the package as abusive">
<a href="/packages//13.0.3/ContactOwners" rel="external nofollow" rel="external nofollow" title="Ask the package owners a question">
<a href="/packages?q=Tags%3A%22json%22" rel="external nofollow" title="Search for json" class="tag">
Then the results of the previous step continue to filter
# Intercept the content of href$substr = $(9) if ($substr -like "/packages/*") { Write-Host $substr }
Output result
/packages//13.0.3" title="13.0.3">
...
/packages//3.5.8" title="3.5.8">
/packages//13.0.3/ReportAbuse" title="Report the package as abusive">
/packages//13.0.3/ContactOwners" title="Ask the package owners a question">
Is it over or not? Come again? Looking at the above results, it's still not enough to filter two unrelated ones.
Get the full content of href
# Find the position of the first double quote$index = $('"') # Get the part/packages//13.0.3$substr = $(0,$index)
Exclude the last two versions of the a tag
# Exclude reports abuse of a tagif ($substr -notlike "*/ReportAbuse") { # Exclude contact author a tagif ($substr -notlike "*/ContactOwners") { # Match version$endIndex = $('/') $startPackageIndex = $endIndex + 1 $packageVersion = $($startPackageIndex) } }
Download and save the file invoke-WebRequest command
# Download address nupkg$packageUrl = "{0}{1}/{2}" -f $httpPrefix,$packageName,$packageVersion # Generate the path to save the file$packageFile = Join-Path -Path $outputDirectory -ChildPath "$packageName.$" # Download .nupkg fileWrite-Host "Downloading $packageName version $packageVersion from $packageUrl" Invoke-WebRequest -Uri $packageUrl -OutFile $packageFile
All codes
# Set the URL of the NuGet package list$packageName = "" $targetHttp = "/packages/" $targetUrl = "{0}{1}" -f $targetHttp, $packageName # Set the directory to save the downloaded package$outputDirectory = "D:\nuget_packages" if (-not (Test-Path $outputDirectory)) { New-Item -Path $outputDirectory -ItemType Directory } # Define download prefix$httpPrefix = "/api/v2/package/" # Download the content of the html file$htmlContent = Invoke-WebRequest -Uri $targetUrl -UseBasicParsing | Select-Object -ExpandProperty Content # Match tags$pattern = "<.*?>" $matches = [regex]::Matches($htmlContent, $pattern) foreach ($match in $matches) { $tag = $ # Get a tag if ($tag -like "<a href=*") { # Get a tag with title if ($tag -like "*title=*") { # Intercept the content of href $substr = $(9) if ($substr -like "/packages/*") { # Find the position of the first double quote $index = $('"') # Get the part/packages//13.0.3 $substr = $(0,$index) # Exclude reports abuse of a tag if ($substr -notlike "*/ReportAbuse") { # Exclude contact author a tag if ($substr -notlike "*/ContactOwners") { # Match version $endIndex = $('/') $startPackageIndex = $endIndex + 1 $packageVersion = $($startPackageIndex) # Download address nupkg $packageUrl = "{0}{1}/{2}" -f $httpPrefix,$packageName,$packageVersion # Generate the path to save the file $packageFile = Join-Path -Path $outputDirectory -ChildPath "$packageName.$" # Download .nupkg file Write-Host "Downloading $packageName version $packageVersion from $packageUrl" Invoke-WebRequest -Uri $packageUrl -OutFile $packageFile } } } } } } # Execution end pause$null = Read-Host
The above is the detailed content of using PowerShell to download all versions of a Nuget package with one click. For more information about downloading all versions of the Nuget package with PowerShell, please follow my other related articles!