Download to Artifactory from a Windows Powershell script

I successfully uploaded a file from Artifactory (Generic Repo) through a WebClient object. I am having trouble downloading a file using the same method. I am trying to figure out the easiest way to download via Powershell to our server.

Please note that installing other utilities, such as Curl, is currently not an option. I am writing automation scripts and want to stick to the base server of Windows 2008 r2 without installing other utilities, since I cannot count on the fact that they are on all servers.

If anyone has an example script using the Rest API, that would be great!

Sample download code (this works):

$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) 

This is an example download code (doesn't work):

 $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) 

This is the error I get from the download:

 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 
+5
source share
3 answers

I tried the Invoke-WebRequest parameter and was able to get it working:

 $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 

You have to create a PSCrendential object so that it does not ask for the user password. But, in addition, this work is exactly as I needed.

+5
source

I do not have Artifactory, but you can try the Invoke-RestMethod PowerShell cmdlet, available in PowerShell v3 and later. Here is an example of how to do this.

You will need credentials and based on their REST documentation, basic authentication of the type that we can get with the -Credential Invoke-RestMethod should cover us.

You also need to provide the message $body your request. Check out the JSON example from your docs , and then edit the $body that I gave as a starting point.

 $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 

I have to say that this is one of the most complex REST API examples I've seen, so to make it easier, I would set curl on the machine and use Fiddler to capture a trace that successfully loads the file. To make things even easier, you can also do this using the Artifactory interface from the browser to download the file and a simple trace record of the download phase. Then take the JSON in the request and use this as a starting point.

+2
source

The cause of your problem is the HTTP method used by UploadFile. By default, UploadFile uses POST, and to upload a file to Artifactory you need to use the PUT method. That's why you get the 405 response "Method not allowed."

To fix this, overload UploadFile with three parameters, as shown here: https://msdn.microsoft.com/en-us/library/ms144230(v=vs.110).aspx

So, the correct version of the code would look like this:

 $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) 
+1
source

Source: https://habr.com/ru/post/1245921/


All Articles