2017-10-16 91 views
1

我试图从Nexus存储库下载最新的神器。如果我给出确切的zip文件名称,它工作正常。当我尝试使用通用URL(REST URI)进行下载时,它会给我401 Unauthorized。我也尝试过Invoke-WebRequestWebClientInvoke-RestMethodPowerShell DownloadFile()不适用于Nexus神器下载

$wc = New-Object System.Net.WebClient 
$URL = "http://nexusrepo/nexus/service/local/artifact/maven/redirect?r=my-snapshot&g=my.group.id&a=my.artifact.id&v=1.10.0-SNAPSHOT&c=win32.win32.x86_64&p=zip" 
$username = "nexus" 
$password = "nexus" 
$auth = $username + ":" + $password 
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true} 
$Encoded = [System.Text.Encoding]::UTF8.GetBytes($auth) 
$EncodedPassword = [System.Convert]::ToBase64String($Encoded) 
$wc.Headers.Add("Accept-Encoding", "gzip,deflate") 
$wc.Credentials = New-Object System.Net.NetworkCredential($username, $password) 
$wc.Headers.Add("Authorization", "Basic " + $EncodedPassword) 
$wc.UseDefaultCredentials = $false 
$wc.DownloadFile($URL, "MyApp.zip") 
 
Exception calling "DownloadString" with "1" argument(s): "The remote server 
returned an error: (401) Unauthorized." 
At C:\temp\NexusDownloadTest\Nexus-Download.ps1:39 char:1 
+ $weburl = $wc.DownloadString($URL) 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : NotSpecified: (:) [], MethodInvocationException 
    + FullyQualifiedErrorId : WebException 

是否有人可以帮助我在此?

+0

Nexus服务器上的日志显示什么? –

+0

我无法访问Nexus服务器的日志。 –

+0

仍在寻找答案。标题信息一旦被重定向就会消失。任何想法我们可以如何在PowerShell中的重定向URL中再次设置Header信息?如果有人能帮助我,请欣赏它。 –

回答

0

作为一种解决方法,我回到了Invoke-WebRequest并首先使用MaximumRedirection 0获取重定向的URL,然后将该URL作为请求提交。下面的代码工作。

$URL = "http://nexusrepo/nexus/service/local/artifact/maven/redirect?r=my-snapshot&g=my.group.id&a=my.artifact.id&v=1.10.0-SNAPSHOT&c=win32.win32.x86_64&p=zip" 
$username = "nexus" 
$password = "nexus" 
$auth=$username+":"+$password 
$Encoded = [System.Text.Encoding]::UTF8.GetBytes($auth) 
$EncodedPassword = [System.Convert]::ToBase64String($Encoded) 
$latestArtifactURL = Invoke-WebRequest $url -Headers @{Authorization = "Basic $EncodedPassword"} -MaximumRedirection 0 
$redirectedMessage = "$latestArtifactURL".IndexOf('http:') 
$targetURL = "$latestArtifactURL".SubString("$redirectedMessage") 
Invoke-WebRequest $targetURL -Headers @{Authorization = "Basic $EncodedPassword"} -OutFile "MyApp.zip" 
相关问题