2017-03-22 35 views
0

我已经在下面的博客中获得了这个脚本,它将使用power-shell脚本将所有nuget包下载到我的本地服务器。我已经使用相同的脚本将Google.Protobuf/3.1.0 Nuget包下载到本地驱动器。这对一个特定的包页面 -使用powershell将Google.Protobuf/3.1.0 nuget包下载到本地机器

PS-1 blog-

# --- settings --- 
$feedUrlBase = "https://www.nuget.org/packages/Google.Protobuf/3.1.0/" 
# the rest will be params when converting to funclet 
$latest = $true 
$overwrite = $false 
$top = 5 #use $top = $null to grab all or a number to get TOP 500 packages 
# $destinationDirectory = join-path ([Environment]::GetFolderPath("MyDocuments")) "NuGetLocal" 
$destinationDirectory = "D:\test" 

# --- locals --- 
$webClient = New-Object System.Net.WebClient 

# --- functions --- 

# download entries on a page, recursively called for page continuations 
function DownloadEntries { 
param ([string]$feedUrl) 
$feed = [xml]$webClient.DownloadString($feedUrl) 
$entries = $feed.feed.entry 
$progress = 0 

foreach ($entry in $entries) { 
    $url = $entry.content.src 
    $fileName = $entry.properties.id + "." + $entry.properties.version + ".nupkg" 
    $saveFileName = join-path $destinationDirectory $fileName 
    $pagepercent = ((++$progress)/$entries.Length*100) 
    if ((-not $overwrite) -and (Test-Path -path $saveFileName)) 
    { 
     write-progress -activity "$fileName already downloaded" -status "$pagepercent% of current page complete" -percentcomplete $pagepercent 
     continue 
    } 
    write-progress -activity "Downloading $fileName" -status "$pagepercent% of current page complete" -percentcomplete $pagepercent 

    [int]$trials = 0 
    do { 
     try { 
      $trials +=1 
      $webClient.DownloadFile($url, $saveFileName) 
      break 
     } catch [System.Net.WebException] { 
      write-host "Problem downloading $url `tTrial $trials `n`tException: " $_.Exception.Message 
     } 
    } 
    while ($trials -lt 3) 
    } 

    $link = $feed.feed.link | where { $_.rel.startsWith("next") } | select href 
    if ($link -ne $null) { 
    # if using a paged url with a $skiptoken like 
    # http:// ... /Packages?$skiptoken='EnyimMemcached-log4net','2.7' 
    # remember that you need to escape the $ in powershell with ` 
    return $link.href 
    } 
    return $null 
} 

# the NuGet feed uses a fwlink which redirects 
# using this to follow the redirect 
function GetPackageUrl { 
param ([string]$feedUrlBase) 
$resp = [xml]$webClient.DownloadString($feedUrlBase) 
return $resp.service.GetAttribute("xml:base") 
} 

# --- do the actual work --- 

# if dest dir doesn't exist, create it 
if (!(Test-Path -path $destinationDirectory)) { 
    New-Item $destinationDirectory -type directory 
} 

# set up feed URL 
$feedUrl = "http://packages.nuget.org/v1/FeedService.svc/Packages" 
if($latest) { 
    $feedUrl = $feedUrl + "?`$filter=IsLatestVersion eq true" 
    if($top -ne $null) { 
     $feedUrl = $feedUrl + "&`$orderby=DownloadCount desc&`$top=$top" 
    } 
} 

while($feedUrl -ne $null) { 
    $feedUrl = DownloadEntries $feedUrl 
} 

PS1-错误日志 -

Problem downloading http://packages.nuget.org/api/v1/package/Newtonsoft.Json/10.0.1  Trial 1 
    Exception: The underlying connection was closed: An unexpected error occurred on a receive. 
Problem downloading http://packages.nuget.org/api/v1/package/Newtonsoft.Json/10.0.1  Trial 2 
    Exception: The underlying connection was closed: An unexpected error occurred on a receive. 
Problem downloading http://packages.nuget.org/api/v1/package/Newtonsoft.Json/10.0.1  Trial 3 
    Exception: The underlying connection was closed: An unexpected error occurred on a receive. 
Problem downloading http://packages.nuget.org/api/v1/package/jQuery/3.1.1 Trial 1 
    Exception: The underlying connection was closed: An unexpected error occurred on a receive. 
Problem downloading http://packages.nuget.org/api/v1/package/jQuery/3.1.1 Trial 2 
    Exception: The operation has timed out 
Problem downloading http://packages.nuget.org/api/v1/package/jQuery/3.1.1 Trial 3 
    Exception: The underlying connection was closed: An unexpected error occurred on a receive. 
Problem downloading http://packages.nuget.org/api/v1/package/EntityFramework/6.1.3 Trial 1 
    Exception: The underlying connection was closed: An unexpected error occurred on a receive. 
Problem downloading http://packages.nuget.org/api/v1/package/EntityFramework/6.1.3 Trial 2 
    Exception: The underlying connection was closed: An unexpected error occurred on a receive. 
Problem downloading http://packages.nuget.org/api/v1/package/EntityFramework/6.1.3 Trial 3 
    Exception: The underlying connection was closed: An unexpected error occurred on a receive. 
Problem downloading http://packages.nuget.org/api/v1/package/Microsoft.AspNet.Mvc/5.2.3  Trial 1 
    Exception: The underlying connection was closed: An unexpected error occurred on a receive. 
Problem downloading http://packages.nuget.org/api/v1/package/Microsoft.AspNet.Mvc/5.2.3  Trial 2 
    Exception: The underlying connection was closed: An unexpected error occurred on a receive. 
Problem downloading http://packages.nuget.org/api/v1/package/Microsoft.AspNet.Mvc/5.2.3  Trial 3 
    Exception: The underlying connection was closed: An unexpected error occurred on a receive. 
Problem downloading http://packages.nuget.org/api/v1/package/Microsoft.AspNet.Razor/3.2.3 Trial 1 
    Exception: The underlying connection was closed: An unexpected error occurred on a receive. 
Problem downloading http://packages.nuget.org/api/v1/package/Microsoft.AspNet.Razor/3.2.3 Trial 2 
    Exception: The underlying connection was closed: An unexpected error occurred on a receive. 
Problem downloading http://packages.nuget.org/api/v1/package/Microsoft.AspNet.Razor/3.2.3 Trial 3 
    Exception: The underlying connection was closed: An unexpected error occurred on a receive. 

回答

0

https://www.nuget.org/packages/Google.Protobuf/3.1.0/不是供稿网址基地脚本。

在我看来是这样的:

$serviceBase = GetPackageUrl($feedUrlBase) 
$feedUrl = $serviceBase + "Packages" 

可以只用

$feedUrl = "http://packages.nuget.org/v1/FeedService.svc/Packages" 

,如果你很高兴来限制自己nuget.org更换......。

+0

我用新的错误日志更新了我的脚本。 – Kally

+0

@Bhave:我的猜测是它没有遵循重定向。 URL本身看起来很好。 –

相关问题