gpt4 book ai didi

powershell - 使用 PowerShell 从 Jenkins 下载工件

转载 作者:行者123 更新时间:2023-12-03 09:59:51 24 4
gpt4 key购买 nike

我尝试使用 PowerShell 从 Jenkins 下载工件,如下所示:

$webClient = new-object System.Net.WebClient 
$webClient.Credentials = New-Object System.Net.NetworkCredential ("username", "password")
$url = "http://jenkins/job/jobName/lastSuccessfulBuild/artifact/*zip*/archive.zip"
$localfilename = "C:\Test\archive.zip"
$webClient.DownloadFile($url, $localfilename)

我得到异常(exception):

Exception calling "DownloadFile" with "2" argument(s): "The remote server retur ned an error: (403) Forbidden." At C:\ps2.ps1:20 char:28 + $webclient.DownloadFile <<<< ($url, $localfilename) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException



如果我尝试使用 wget 下载工件,它会起作用:
wget --auth-no-challenge --http-user=username --http-password=password http://jenkins/job/jobName/lastSuccessfulBuild/artifact/*zip*/archive.zip

如果我使用没有参数的 wget --auth-no-challenge我得到同样的错误 - Forbidden .

最佳答案

在请求 header 中使用此授权效果很好:

# Create web client with authorization header
$webClient = new-object System.Net.WebClient
$credentialAsBytes = [System.Text.Encoding]::ASCII.GetBytes($userName + ":" + $password)
$credentialAsBase64String = [System.Convert]::ToBase64String($credentialAsBytes);
$webClient.Headers[[System.Net.HttpRequestHeader]::Authorization] = "Basic " + $credentialAsBase64String;

关于powershell - 使用 PowerShell 从 Jenkins 下载工件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21426795/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com