簡體   English   中英

從 Windows Powershell 腳本上傳到 Artifactory

[英]Uploading to Artifactory from a Windows Powershell script

我已經通過 WebClient 對象從 Artifactory (Generic Repo) 成功下載了一個文件。 我在通過相同方法上傳文件時遇到問題。 我試圖找出通過 Powershell 上傳到我們服務器的最簡單方法。

請注意,此時不能選擇安裝其他實用程序,例如 Curl。 我正在編寫自動化腳本並希望堅持使用基本的 Windows 2008 r2 服務器,不安裝其他實用程序,因為我不能指望它們在所有服務器上都存在。

如果有人有一個使用 Rest API 的示例腳本,那將是完美的!

下載代碼示例(有效):

$SOURCE = "https://artifactory.example.com/artifactory/net-generic-local/APP/BF_1.0.zip"  
$DESTINATION = ".\BF_1.0.zip"  
$AF_USER ="user"  
$AF_PWD ="password"  
$WebClient = New-Object System.Net.WebClient  
$WebClient.Credentials = New-Object System.Net.NetworkCredential($AF_USER,$AF_PWD)  
$WebClient.DownloadFile($SOURCE,$DESTINATION)  

這是上傳代碼的示例(不起作用):

$SOURCE = ".\BF_2.0.zip"  
$DESTINATION = "https://artifactory.example.com/artifactory/net-generic-local/APP/BF_2.0.zip"  
$AF_USER ="user"  
$AF_PWD ="password"  
$WebClient = New-Object System.Net.WebClient  
$WebClient.Credentials = New-Object System.Net.NetworkCredential($AF_USER, $AF_PWD)  
$URI = New-Object System.Uri($DESTINATION)  
$WebClient.UploadFile($URI,$SOURCE)  

這是我從上傳中得到的錯誤:

Exception calling "UploadFile" with "2" argument(s): "The remote server returned an error: (405) Method Not Allowed."  
At E:\transient\af_put.ps1:8 char:1  
+ $WebClient.UploadFile($URI,$SOURCE)  
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException  
    + FullyQualifiedErrorId : WebException  

我嘗試了 Invoke-WebRequest 選項並且能夠讓它工作:

$URI = New-Object System.Uri("https://artifactory.example.com/artifactory/net-generic-local/APP/BF_2.0.zip")  
$SOURCE = ".\BF_2.0.zip"  
$AF_USER = "user"  
$AF_PWD = ConvertTo-SecureString "password" -AsPlainText -Force  
$CREDS = New-Object System.Management.Automation.PSCredential ($AF_USER, $AF_PWD)  

Invoke-WebRequest -Uri $URI -InFile $SOURCE -Method Put -Credential $CREDS  

必須創建一個 PSCrendential 對象,這樣它就不會提示輸入用戶密碼。 但除此之外,這完全符合我的需要。

您的問題的原因是 UploadFile 使用的 HTTP 方法。 默認情況下,UploadFile 使用 POST,而要將文件上傳到 Artifactory,您需要使用 PUT 方法。 這就是為什么您會收到 405“Method Not Allowed”響應。

要修復此使用 UploadFile 重載的三個參數,如下所示: https : //msdn.microsoft.com/en-us/library/ms144230(v= vs.110).aspx

因此,正確版本的代碼將如下所示:

$SOURCE = ".\BF_2.0.zip"  
$DESTINATION = "https://artifactory.example.com/artifactory/net-generic-local/APP/BF_2.0.zip"  
$AF_USER ="user"  
$AF_PWD ="password"  
$WebClient = New-Object System.Net.WebClient  
$WebClient.Credentials = New-Object System.Net.NetworkCredential($AF_USER, $AF_PWD)  
$URI = New-Object System.Uri($DESTINATION)
$METHOD = "PUT"  
$WebClient.UploadFile($URI, $METHOD, $SOURCE)  

我手邊沒有 Artifactory,但您可能想嘗試Invoke-RestMethod PowerShell cmdlet,它在 PowerShell v3 及更高版本的框中可用。 這是如何做到這一點的示例。

您將需要憑據,並且根據他們的 REST 文檔,我們可以使用Invoke-RestMethod-Credential參數獲得的類型的基本身份驗證應該涵蓋我們那里。

您還需要在您的請求中提供消息$body 從他們的文檔中查看此處的 JSON 示例,然后編輯我作為起點給出的$body

$credential = Get-Credential
$body = @{action="Upload";param2="Data";param3="Data";} | ConvertTo-Json 
Invoke-RestMethod -uri "http://localhost:8080/artifactory/api/build" `
  -ContentType "application/json" -method POST -body $body -Credential

我必須說,這是我見過的更復雜的 REST API 示例之一,因此為了更簡單,我將在機器上安裝 curl 並使用 Fiddler 捕獲成功上傳文件的跟蹤。 為了讓事情變得更簡單,您還可以使用瀏覽器中的 Artifactory UI 來上傳文件,並簡單地記錄上傳步驟​​的痕跡。 然后,獲取請求中的 JSON 並將其用作起點。

JFrog 知識庫提供了一個通過 PowerShell 上傳到 Artifactory 的示例 此示例使用了 FoxDeploy 上一個答案中的Invoke-RestMethod ,但使用了-InFile參數和不同的內容類型,如下所示:

Invoke-RestMethod -uri <complete URI to where the artifact will be in Artifactory>
  -Method Put -InFile <path of file to upload> -Credential <PS creds>
  -ContentType "multipart/form-data" -TimeoutSec <in seconds>

我的答案當然是派生的,但是當使用 API 令牌進行身份驗證將某些東西從 powershell 推送到我的 Artifactory 存儲庫時,這對我有用:

$credential_bytes = [System.Text.Encoding]::UTF8.GetBytes($username + ":" + $api_token)
$credentials = [System.Convert]::ToBase64String($credential_bytes)
$credential_header = "Basic " + $credentials    
Invoke-WebRequest -Uri $artifactory_dest_url -InFile "my_file.zip" -Method Put -Headers @{"Authorization"="$credential_header"}

嘆。 我很遺憾我正在使用 Powershell。 我的生命在做什么? 無論如何,它有效。 繼續。

FoxDeploy 和 Maclnos 的道具。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM