113人参与 • 2025-02-13 • Powershell
一转眼好几年没有写博客了,来博客园冒个泡,最近由于工作需要,内网办公,幸运的是只需要上传一个*.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) } }
# 下载地址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包所有版本的资料请关注代码网其它相关文章!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论