利用PowerShell一鍵下載Nuget某個(gè)包的所有版本
一轉(zhuǎn)眼好幾年沒(méi)有寫(xiě)博客了,來(lái)博客園冒個(gè)泡,最近由于工作需要,內(nèi)網(wǎng)辦公,幸運(yùn)的是只需要上傳一個(gè)*.nupkg一個(gè)包信息就可以在私有nuget下載到了,下面就用PowerShell編寫(xiě)下載腳本,需要注意的是PowerShell后綴ps1(最后一個(gè)數(shù)字1),以Newtonsoft.Json為例:
下載地址
# 設(shè)置NuGet包列表的URL $packageName = "Newtonsoft.Json" $targetHttp = "https://www.nuget.org/packages/" $targetUrl = "{0}{1}" -f $targetHttp, $packageName
保存地址
# 設(shè)置保存已下載包的目錄 $outputDirectory = "D:\nuget_packages" if (-not (Test-Path $outputDirectory)) { New-Item -Path $outputDirectory -ItemType Directory }
解析下載版本地址
定義下載需要解析的包地址
# 定義下載前綴 $httpPrefix = "https://www.nuget.org/api/v2/package/" # 下載html文件內(nèi)容 $htmlContent = Invoke-WebRequest -Uri $targetUrl -UseBasicParsing | Select-Object -ExpandProperty Content # 匹配標(biāo)簽 $pattern = "<.*?>" $matches = [regex]::Matches($htmlContent, $pattern)
獲取所有a標(biāo)簽
foreach ($match in $matches) { $tag = $match.Value # 獲取a標(biāo)簽 if ($tag -like "<a href=*") { Write-Host $tag } }
輸出結(jié)果
<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">
...
觀察上一步結(jié)果可以看出來(lái)每一個(gè)版本都有title,且title內(nèi)容是版本
# 獲取含有title的a標(biāo)簽 if ($tag -like "*title=*") { Write-Host $tag }
輸出結(jié)果
<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">
接著上一步的結(jié)果繼續(xù)過(guò)濾
# 截取href的內(nèi)容 $substr = $tag.Substring(9) if ($substr -like "/packages/*") { Write-Host $substr }
輸出結(jié)果
/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">
有完沒(méi)完,又來(lái)了?看上面的結(jié)果就還差過(guò)濾兩個(gè)不相關(guān)的了,
獲取href完整內(nèi)容
# 查找第一個(gè)雙引號(hào)的位置 $index = $substr.IndexOf('"') # 獲取部分/packages/Newtonsoft.Json/13.0.3 $substr = $substr.Substring(0,$index)
剔除最后兩個(gè)版本無(wú)關(guān)的a標(biāo)簽
# 排除報(bào)告濫用a標(biāo)簽 if ($substr -notlike "*/ReportAbuse") { # 排除聯(lián)系作者a標(biāo)簽 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
全部代碼
# 設(shè)置NuGet包列表的URL $packageName = "Newtonsoft.Json" $targetHttp = "https://www.nuget.org/packages/" $targetUrl = "{0}{1}" -f $targetHttp, $packageName # 設(shè)置保存已下載包的目錄 $outputDirectory = "D:\nuget_packages" if (-not (Test-Path $outputDirectory)) { New-Item -Path $outputDirectory -ItemType Directory } # 定義下載前綴 $httpPrefix = "https://www.nuget.org/api/v2/package/" # 下載html文件內(nèi)容 $htmlContent = Invoke-WebRequest -Uri $targetUrl -UseBasicParsing | Select-Object -ExpandProperty Content # 匹配標(biāo)簽 $pattern = "<.*?>" $matches = [regex]::Matches($htmlContent, $pattern) foreach ($match in $matches) { $tag = $match.Value # 獲取a標(biāo)簽 if ($tag -like "<a href=*") { # 獲取含有title的a標(biāo)簽 if ($tag -like "*title=*") { # 截取href的內(nèi)容 $substr = $tag.Substring(9) if ($substr -like "/packages/*") { # 查找第一個(gè)雙引號(hào)的位置 $index = $substr.IndexOf('"') # 獲取部分/packages/Newtonsoft.Json/13.0.3 $substr = $substr.Substring(0,$index) # 排除報(bào)告濫用a標(biāo)簽 if ($substr -notlike "*/ReportAbuse") { # 排除聯(lián)系作者a標(biāo)簽 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 } } } } } } # 執(zhí)行結(jié)束暫停 $null = Read-Host
以上就是利用PowerShell一鍵下載Nuget某個(gè)包的所有版本的詳細(xì)內(nèi)容,更多關(guān)于PowerShell下載Nuget包所有版本的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Windows Powershell Foreach 循環(huán)
Foreach-object 為cmdlet命令,使用在管道中,對(duì)管道結(jié)果逐個(gè)處理,foreach為遍歷集合的關(guān)鍵字。2014-10-10PowerShell常用正則表達(dá)式和語(yǔ)法參考
這篇文章主要介紹了PowerShell常用正則表達(dá)式和語(yǔ)法參考,主要介紹PowerShell中的正則表達(dá)式和其含義,需要的朋友可以參考下2014-07-07PowerShell函數(shù)實(shí)現(xiàn)類(lèi)似重載功能實(shí)例
這篇文章主要介紹了PowerShell函數(shù)實(shí)現(xiàn)類(lèi)似重載功能實(shí)例,PowerShell函數(shù)是不支持重載的,本文介紹的是類(lèi)似功能,需要的朋友可以參考下2014-07-07PowerShell中運(yùn)行CMD命令的技巧總結(jié)(解決名稱(chēng)沖突和特殊字符等問(wèn)題)
這篇文章主要介紹了PowerShell中運(yùn)行CMD命令的技巧總結(jié)(解決名稱(chēng)沖突和特殊字符等問(wèn)題),需要的朋友可以參考下2014-05-05Powershell直接腳本時(shí)出現(xiàn)無(wú)法加載文件因?yàn)榻箞?zhí)行腳本
Powershell直接腳本時(shí)出現(xiàn)無(wú)法加載文件因?yàn)樵诖讼到y(tǒng)中禁止執(zhí)行腳本,有關(guān)此問(wèn)題的解決方法如下2014-08-08PowerShell實(shí)現(xiàn)測(cè)試端口可用性腳本分享
這篇文章主要介紹了PowerShell實(shí)現(xiàn)測(cè)試端口可用性腳本分享,本文腳本相對(duì)簡(jiǎn)單,使用TCP套接字實(shí)現(xiàn)需求,需要的朋友可以參考下2014-11-11PowerShell小技巧之實(shí)現(xiàn)文件下載(類(lèi)wget)
在.NET環(huán)境下提到下載文件大多數(shù)人熟悉的是通過(guò)System.Net.WebClient進(jìn)行下載,這個(gè)程序集能實(shí)現(xiàn)下載的功能,但是有缺陷,事實(shí)上微軟也提供了避免這些缺陷的程序集System.Net.HttpWebRequest和HttpWebResponse,本文將會(huì)使用這兩個(gè)程序集來(lái)實(shí)現(xiàn)PowerShell版wget的功能。2014-10-10