Location>code7788 >text

PowerShell one-click download of all versions of a Nuget package

Popularity:260 ℃/2024-12-10 22:01:00

As a result of work needs, intranet office, fortunately only need to upload a *.nupkg a package information can be downloaded in the private nuget, the following on the use of PowerShell to write download scripts, need to pay attention to the PowerShell extensionps1(last digit 1), as an example:

download address

# Setting the URL of the NuGet package list
$packageName = ""
$targetHttp = "/packages/"
$targetUrl = "{0}{1}" -f $targetHttp, $packageName

Save Address

# Set the directory where downloaded packages are saved
$outputDirectory = "D:\nuget_packages"
if (-not (Test-Path $outputDirectory)) {
    New-Item -Path $outputDirectory -ItemType Directory
}

Analyze the download version address

Define the address of the package to be parsed for download

# Define download prefixes
$httpPrefix = "/api/v2/package/"
# Download html file content
$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 = $match.Value
# Get a tag
if ($tag -like "<a href=*") {
Write-Host $tag
}
}

output result

<a href="#" id="skipToContent" class="showOnFocus" title="Skip To Content">
...
<a href="/packages//">
<a href="/packages//13.0.3" title="13.0.3">
...
<a href="/packages//3.5.8" title="3.5.8">
<a href="/stats/packages/?groupby=Version" title="Package Statistics">
...
<a href="/packages//13.0.3/ReportAbuse" title="Report the package as abusive">
<a href="/packages//13.0.3/ContactOwners" title="Ask the package owners a question">
...

Observing the results of the previous step it can be seen that each version has a title and the title content is the version

# Get the a tag with title
if ($tag -like "*title=*") {
  Write-Host $tag        
}

output result

<a href="#" id="skipToContent" class="showOnFocus" title="Skip To Content">
<a href="/packages//13.0.3" title="13.0.3">
...
<a href="/packages//3.5.8" title="3.5.8">
<a href="/stats/packages/?groupby=Version" title="Package Statistics">
<a href="/json" data-track="outbound-project-url" title="Visit the project site to learn more about this package" rel="nofollow">
...
<a href="/packages//13.0.3/ReportAbuse" title="Report the package as abusive"> <a href="/packages//13.0.3/ContactOwners" title="Ask the package owners a question"> <a href="/packages?q=Tags%3A%22json%22" title="Search for json" class="tag">

Continue filtering with the results of the previous step

# Intercept the content of href
$substr = $tag.Substring(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">

Are you done? Again? Look at the results above. We're just short of filtering out two irrelevant ones.

Get full content of href

# Find the location of the first double quote
$index = $substr.IndexOf('"')
# Get section /packages//13.0.3
$substr = $(0,$index)

Stripping out the last two versions of irrelevant a tags

# Exclusion report misuse of a-tags
if ($substr -notlike "*/ReportAbuse") { # Exclusion of contact author a tag if ($substr -notlike "*/ContactOwners") { # Matching version
$endIndex = $substr.LastIndexOf('/') $startPackageIndex = $endIndex + 1 $packageVersion = $substr.Substring($startPackageIndex) } }

Invoke-WebRequest command to download and save a file

# Download nupkg
$packageUrl = "{0}{1}/{2}" -f $httpPrefix,$packageName,$packageVersion # Generate path to save file $packageFile = Join-Path -Path $outputDirectory -ChildPath "$packageName.$" # Download the .nupkg file Write-Host "Downloading $packageName version $packageVersion from $packageUrl" Invoke-WebRequest -Uri $packageUrl -OutFile $packageFile

All Codes

# Setting the URL of the NuGet package list
$packageName = ""
$targetHttp = "/packages/"
$targetUrl = "{0}{1}" -f $targetHttp, $packageName
# Set the directory where downloaded packages are saved
$outputDirectory = "D:\nuget_packages"
if (-not (Test-Path $outputDirectory)) {
    New-Item -Path $outputDirectory -ItemType Directory
}
# Define download prefixes
$httpPrefix = "/api/v2/package/"
# Download html file content
$htmlContent = Invoke-WebRequest -Uri $targetUrl -UseBasicParsing | Select-Object -ExpandProperty Content
# Match Tags
$pattern = "<.*?>"
$matches = [regex]::Matches($htmlContent, $pattern)
foreach ($match in $matches) {
    $tag = $match.Value
        # Get a tag
        if ($tag -like "<a href=*") {
            # Get the a tag with title
            if ($tag -like "*title=*")
            {
                # Intercept the content of href
                $substr = $tag.Substring(9)
                if ($substr -like "/packages/*")
                {
                    # Find the location of the first double quote
                    $index = $substr.IndexOf('"')
                    # Get section /packages//13.0.3
                    $substr = $(0,$index)
                    # Exclude reports abusing the a tag
                    if ($substr -notlike"*/ReportAbuse")
                    {
                        # Exclude contact author a tag
                        if ($substr -notlike"*/ContactOwners")
                        {
                            # Matching version
                            $endIndex = $('/')
                            $startPackageIndex = $endIndex + 1
                            $packageVersion = $($startPackageIndex)
                            # download addressnupkg
                            $packageUrl ="{0}{1}/{2}" -f $httpPrefix,$packageName,$packageVersion
                            # Generate the path to the save file
                            $packageFile = Join-Path -Path $outputDirectory -ChildPath"$packageName.$packageVersion.nupkg"
                            # Download the .nupkg file
                            Write-Host"Downloading $packageName version $packageVersion from $packageUrl"
              Invoke-WebRequest -Uri $packageUrl -OutFile $packageFile

                        }

                    }

                }

            }

        }
}
# End-of-execution pause
$null = Read-Host
View Code