利用PowerShell一键下载Nuget某个包的所有版本
一转眼好几年没有写博客了,来博客园冒个泡,最近由于工作需要,内网办公,幸运的是只需要上传一个*.nupkg一个包信息就可以在私有nuget下载到了,下面就用PowerShell编写下载脚本,需要注意的是PowerShell后缀ps1(最后一个数字1),以Newtonsoft.Json为例:
下载地址
# 设置NuGet包列表的URL
$packageName = "Newtonsoft.Json"
$targetHttp = "https://www.nuget.org/packages/"
$targetUrl = "{0}{1}" -f $targetHttp, $packageName保存地址
# 设置保存已下载包的目录
$outputDirectory = "D:\nuget_packages"
if (-not (Test-Path $outputDirectory)) {
New-Item -Path $outputDirectory -ItemType Directory
}解析下载版本地址
定义下载需要解析的包地址
# 定义下载前缀 $httpPrefix = "https://www.nuget.org/api/v2/package/" # 下载html文件内容 $htmlContent = Invoke-WebRequest -Uri $targetUrl -UseBasicParsing | Select-Object -ExpandProperty Content # 匹配标签 $pattern = "<.*?>" $matches = [regex]::Matches($htmlContent, $pattern)
获取所有a标签
foreach ($match in $matches) {
$tag = $match.Value
# 获取a标签
if ($tag -like "<a href=*") {
Write-Host $tag
}
}输出结果
<a href="#" rel="external nofollow" rel="external nofollow" id="skipToContent" class="showOnFocus" title="Skip To Content">
...
<a href="/packages/System.Xml.XmlDocument/" rel="external nofollow" >
<a href="/packages/Newtonsoft.Json/13.0.3" rel="external nofollow" rel="external nofollow" title="13.0.3">
...
<a href="/packages/Newtonsoft.Json/3.5.8" rel="external nofollow" rel="external nofollow" title="3.5.8">
<a href="/stats/packages/Newtonsoft.Json?groupby=Version" rel="external nofollow" rel="external nofollow" title="Package Statistics">
...
<a href="/packages/Newtonsoft.Json/13.0.3/ReportAbuse" rel="external nofollow" rel="external nofollow" title="Report the package as abusive">
<a href="/packages/Newtonsoft.Json/13.0.3/ContactOwners" rel="external nofollow" rel="external nofollow" title="Ask the package owners a question">
...
观察上一步结果可以看出来每一个版本都有title,且title内容是版本
# 获取含有title的a标签
if ($tag -like "*title=*") {
Write-Host $tag
}输出结果
<a href="#" rel="external nofollow" rel="external nofollow" id="skipToContent" class="showOnFocus" title="Skip To Content">
<a href="/packages/Newtonsoft.Json/13.0.3" rel="external nofollow" rel="external nofollow" title="13.0.3">
...
<a href="/packages/Newtonsoft.Json/3.5.8" rel="external nofollow" rel="external nofollow" title="3.5.8">
<a href="/stats/packages/Newtonsoft.Json?groupby=Version" rel="external nofollow" rel="external nofollow" title="Package Statistics">
<a href="https://www.newtonsoft.com/json" rel="external nofollow" data-track="outbound-project-url" title="Visit the project site to learn more about this package" >
...
<a href="/packages/Newtonsoft.Json/13.0.3/ReportAbuse" rel="external nofollow" rel="external nofollow" title="Report the package as abusive">
<a href="/packages/Newtonsoft.Json/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">
接着上一步的结果继续过滤
# 截取href的内容
$substr = $tag.Substring(9)
if ($substr -like "/packages/*") {
Write-Host $substr
}输出结果
/packages/Newtonsoft.Json/13.0.3" title="13.0.3">
...
/packages/Newtonsoft.Json/3.5.8" title="3.5.8">
/packages/Newtonsoft.Json/13.0.3/ReportAbuse" title="Report the package as abusive">
/packages/Newtonsoft.Json/13.0.3/ContactOwners" title="Ask the package owners a question">
有完没完,又来了?看上面的结果就还差过滤两个不相关的了,
获取href完整内容
# 查找第一个双引号的位置
$index = $substr.IndexOf('"')
# 获取部分/packages/Newtonsoft.Json/13.0.3
$substr = $substr.Substring(0,$index)剔除最后两个版本无关的a标签
# 排除报告滥用a标签
if ($substr -notlike "*/ReportAbuse") {
# 排除联系作者a标签
if ($substr -notlike "*/ContactOwners") {
# 匹配版本
$endIndex = $substr.LastIndexOf('/')
$startPackageIndex = $endIndex + 1
$packageVersion = $substr.Substring($startPackageIndex)
}
}Invoke-WebRequest命令下载并保存文件
# 下载地址nupkg
$packageUrl = "{0}{1}/{2}" -f $httpPrefix,$packageName,$packageVersion
# 生成保存文件的路径
$packageFile = Join-Path -Path $outputDirectory -ChildPath "$packageName.$packageVersion.nupkg"
# 下载 .nupkg 文件
Write-Host "Downloading $packageName version $packageVersion from $packageUrl"
Invoke-WebRequest -Uri $packageUrl -OutFile $packageFile全部代码
# 设置NuGet包列表的URL
$packageName = "Newtonsoft.Json"
$targetHttp = "https://www.nuget.org/packages/"
$targetUrl = "{0}{1}" -f $targetHttp, $packageName
# 设置保存已下载包的目录
$outputDirectory = "D:\nuget_packages"
if (-not (Test-Path $outputDirectory)) {
New-Item -Path $outputDirectory -ItemType Directory
}
# 定义下载前缀
$httpPrefix = "https://www.nuget.org/api/v2/package/"
# 下载html文件内容
$htmlContent = Invoke-WebRequest -Uri $targetUrl -UseBasicParsing | Select-Object -ExpandProperty Content
# 匹配标签
$pattern = "<.*?>"
$matches = [regex]::Matches($htmlContent, $pattern)
foreach ($match in $matches) {
$tag = $match.Value
# 获取a标签
if ($tag -like "<a href=*") {
# 获取含有title的a标签
if ($tag -like "*title=*")
{
# 截取href的内容
$substr = $tag.Substring(9)
if ($substr -like "/packages/*")
{
# 查找第一个双引号的位置
$index = $substr.IndexOf('"')
# 获取部分/packages/Newtonsoft.Json/13.0.3
$substr = $substr.Substring(0,$index)
# 排除报告滥用a标签
if ($substr -notlike "*/ReportAbuse")
{
# 排除联系作者a标签
if ($substr -notlike "*/ContactOwners")
{
# 匹配版本
$endIndex = $substr.LastIndexOf('/')
$startPackageIndex = $endIndex + 1
$packageVersion = $substr.Substring($startPackageIndex)
# 下载地址nupkg
$packageUrl = "{0}{1}/{2}" -f $httpPrefix,$packageName,$packageVersion
# 生成保存文件的路径
$packageFile = Join-Path -Path $outputDirectory -ChildPath "$packageName.$packageVersion.nupkg"
# 下载 .nupkg 文件
Write-Host "Downloading $packageName version $packageVersion from $packageUrl"
Invoke-WebRequest -Uri $packageUrl -OutFile $packageFile
}
}
}
}
}
}
# 执行结束暂停
$null = Read-Host以上就是利用PowerShell一键下载Nuget某个包的所有版本的详细内容,更多关于PowerShell下载Nuget包所有版本的资料请关注脚本之家其它相关文章!
相关文章
PowerShell包含另一个脚本文件和获取当前脚本所在目录的方法例子
这篇文章主要介绍了PowerShell包含另一个脚本文件和获取当前脚本所在目录的方法例子,需要的朋友可以参考下2014-08-08
PowerShell小技巧之使用New-Module命令动态创建对象
这篇文章主要介绍了在PowerShell中使用New-Module命令动态创建对象,比New-Object高大上很多了吧2014-09-09
使用PowerShell脚本备份和清理Windows事件日志
PowerShell 是一个强大的命令行工具,允许系统管理员可以自动执行许多日常任务,包括管理 Windows 事件日志,在这个脚本中,我们将创建一个 PowerShell 脚本将所有事件日志备份到指定位置,然后清除日志,以释放磁盘空间,提高系统性能,需要的朋友可以参考下2025-11-11
Windows Powershell ForEach-Object 循环
Powershell管道就像流水线,对于数据的处理是一个环节接着一个环节,如果你想在某一环节对流进来的数据逐个细致化的处理,可是使用ForEach-Object,$_ 代表当前的数据。2014-10-10
PowerShell脚本实现网卡DHCP自动获取IP地址、设置静态IP地址的方法
这篇文章主要介绍了PowerShell脚本实现网卡DHCP自动获取IP地址、设置静态IP地址的方法,本文同时讲解了用PowerShell设置网卡DHCP、静态IP的方法,需要的朋友可以参考下2014-08-08


最新评论