ARTIFACTORY: How to download or upload a file from/to Artifactory using PowerShell

DaYoun Kang
2022-06-14 12:45

When using Artifactory with PowerShell, Invoke-WebRequest and Invoke-RestMethod can be used to deploy and resolve a file. 

Download a File From Artifactory

In order to download a file from Artifactory, you can refer to the below example:Invoke-WebRequest -Uri http://[ARTIFACTORY_URL]/artifactory/generic-local/a.txt -OutFile "a.txt" -Headers @{ Authorization = "Basic "+ [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("USERNAME:PASSWORD")) }
With the above example, I can download “a.txt” under my “generic-local” repository from my Artifactory using my username and password.

If you’d like to use an api key, you can change the header like below:Invoke-WebRequest -Uri http://[ARTIFACTORY_URL]/artifactory/generic-local/a.txt -OutFile "a.txt" -Headers @{ "APIKey" = "AKC...WP"} 

Upload a File to Artifactory

To upload a file to Artifactory, you can use the “Invoke-RestMethod” like below:Invoke-RestMethod -Uri http://[ARTIFACTORY_URL]/artifactory/generic-local/b.txt  -Method Put -InFile "b.txt" -Headers @{ Authorization = "Basic "+ [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("USERNAME:PASSWORD")) }
With the above example, I can upload “b.txt” to my “generic-local” repository using my username and password.

Note that if you’re working with Artifactory version below 7.29.0, add the below line to $JFROG_HOME/artifactory/var/etc/artifactory/ file as for those versions native browser redirect prevents Windows PowerShell (Invoke-WebRequest command) from downloading a restart is required to apply the above change.