Download Artifactory Folder as an Archive using PowerShell - powershell

I'm trying to download an Artifactory folder as a tar.gz file using the PowerShell command line. In Bash, the following command does the job:
curl -i -v -XGET -H 'X-JFrog-Art-Api:<my API key>' "https://artifactory.<myrepo.com>/path/to/files?archiveType=tar.gz" --output ./file_location/filename.tar.gz
In PowerShell, however, this doesn't seem to work:
Invoke-RestMethod -uri "https://artifactory.<myrepo.com>/path/to/files?archiveType=tar.gz" -Method 'Get' -OutFile .\file_location\filename.tar.gz -H #{'X-JFrog-Art-Api' ='<my API key>';'Content-Type'='multipart/form-data'}
This operation creates filename.tar.gz, but the file is just a HTML file giving the directory structure rather than the actual files.
If I only want an individual file, I can use PowerShell to download files (not an archive) with:
Invoke-RestMethod -uri "https://artifactory.<myrepo.com>/path/to/files/filename.xyz" -Method 'Get' -OutFile .\file_location\filename.xyz -H #{'X-JFrog-Art-Api' ='<my API key>';'Content-Type'='multipart/form-data'}
And filename.xyz is transferred to the local machine. So I'm a bit confused with why the tool to make an archive fails.

I figured it out!
Invoke-RestMethod -Uri "https://my-artifactory-server/artifactory/api/archive/download/path/to/some/folder?archiveType=zip -Method "Get" -Headers #{"Authorization"="Basic $my_artifactory_api_token"; 'Content-Type'='multipart/form-data'} -Outfile c:\destination_location\folder.zip
You need to add "api/archive/download" to the Uri, after the hostname.

Related

Translate cURL -F (--form) statement to PowerShell

I have a working cURL statement that takes the contents of a .csv file and posts them to a REST endpoint:
curl \
'https://app.sleuth.io/api/1/deployments/<ORG_SLUG>/<DEPLOYMENT_SLUG>/import_deploys' \
-H 'Authorization: apikey <APIKEY>' \
-F 'csv_file=#<FILENAME>.csv'
I am trying to convert it to PowerShell format, but can't seem to get there.
I am fairly new to APIs, so I've been using the Curl2PS tool for translating cURL commands to PowerShell, and it worked fairly well with other types, but it struggles with the -F (--form) parameter. This is what I get when I input the above cURL statement:
WARNING: The parameter '-F' is not yet supported. Please refer to curl man pages: https://curl.se/docs/manpage.html
Invoke-RestMethod -Method Get -Uri 'https://app.sleuth.io/api/1/deployments/<ORG_SLUG>/<DEPLOYMENT_SLUG>/import_deploys' -Verbose:$false -Headers #{
'Authorization' = 'apikey <APIKEY>'
}
It basically only does a partial job at converting the statement and skips the -F part completely, leaving it blank.
After some googling I tried using the -Infile method, but I probably did it wrong (again, noob here):
Invoke-RestMethod -Method POST -Uri 'https://app.sleuth.io/api/1/deployments/<ORG_SLUG>/<DEPLOYMENT_SLUG>/import_deploys'-Verbose -Headers #{
'Authorization' = 'apikey <APIKEY>'
'Content-Type' = 'multipart/form-data'
} -Infile '<FILENAME>.csv'
Unfortunately, I get a Bad Request (400) error.
I tried googling for a solution, but couldn't really find one, so any input here would be much appreciated.

Powershell equivalent to curl -F to upload a file

I need to upload files via commandline / powershell.
It works fine with
curl -F "file=#test.txt" https://api.anonfiles.com/upload
However, curl does not exist on Windows Server 2016 and I do not want to tell my clients to set it up. So I am looking for a powershell alternative to accomplish this task. I tried various solutions, but none of them worked. What I tried so far:
(1)
$postParams = #{file='C:\users\user\testfile.txt'}; Invoke-WebRequest -Uri https://api.anonfiles.com/upload -Method POST -Body $postParams
(2)
Invoke-WebRequest -UseBasicParsing https://api.anonfiles.com/upload -ContentType "application/json" -Method POST -InFile "C:\users\user\testfile.txt"
(3)
$file=[io.file]::readallbytes('c:\users\user\testfile.txt')
Invoke-WebRequest -UseBasicParsing https://api.anonfiles.com/upload -ContentType "application/json" -Method POST -Body "{ '#file':'"$file"'}"
None of it works. I canot believe it's so hard to replace an curl oneliner in powershell... The error in each case is a 400 http error, the request is wrong.
How do I send the above mentioned curl-request equivalent in powershell? The site is https://anonfiles.com/docs/api
I gave up. It's not possible in Powershell.
My App will just download curl.exe as standalone from now on, if it's not there.

How to download a file accepting license using powershell

I am trying to download package from the below link using powershell.
https://www.tenable.com/downloads/nessus-agents
i do not have direct link for these package also when i click on download it ask to agree. I was able to do it on Linux using command shown below. Kindly advise how can i do it in windows.
"wget --no-check-certificate --post-data='accept="I accept the terms of this license"&x=""&sid=5mcia8gchg28attkc9oarah153&p=NessusAgent-7.4.2-amzn.x86_64.rpm' 'https://www.tenable.com/downloads/nessus-agents' -O NessusAgent-7.4.2-amzn.x86_64.rpm"
could not find anything tried option with invoke-webrequest
Invoke-RestMethod -Uri 'https://www.tenable.com/downloads/nessus-agents'
There's a GET query string parameter that indicates acceptance.
Simply add i_agree_to_tenable_license_agreement=true to your query string parameters.
Invoke-WebRequest -Uri 'https://www.tenable.com/downloads/api/v1/public/pages/nessus-agents/downloads/9762/download?i_agree_to_tenable_license_agreement=true' -OutFile 'NessusAgent-7.4.2-x64.msi'
You can easily get the IDs of the other files from their API endpoint like so:
(Invoke-WebRequest -Uri 'https://www.tenable.com/downloads/api/v1/public/pages/nessus-agents' | ConvertFrom-Json).downloads | Format-Table -AutoSize
This is similar syntax in Powershell, but it's just downloading a file with contents "OK".
$body = 'accept="I accept the terms of this license"&x=""&sid=5mcia8gchg28attkc9oarah153&p=NessusAgent-7.4.2-amzn.x86_64.rpm'
$uri = 'https://www.tenable.com/downloads/nessus-agents'
$resp = Invoke-WebRequest -Method Post -Body $body -Uri $uri -OutFile .\NessusAgent-7.4.2-amzn.x86_64.rpm
Maybe the "sid" variable needs to change per request.

How to HTTP PUT a file in Powershell and capture response body?

I'd like to upload a file using powershell and capture the resulting response body. How can one do this?
I've tried:
$env:DEPLOY_URL = & curl.exe https://make.mudlet.org/snapshots/Mudlet-$env:VERSION$env:MUDLET_VERSION_BUILD-windows.zip --upload-file Mudlet-$env:VERSION$env:MUDLET_VERSION_BUILD-windows.zip 2>&1
But that does not work and says Command executed with exception: without listing the exception.
Explore using the Invoke-RestMethod command. You can save the response body using the -OutFile parameter.
An example PUT would be as follows where $uri is the destination, $inFile is the path to the local file, and $outFile is the path to where to save the response body:
Invoke-RestMethod -Uri $uri -Method PUT -InFile $inFile -OutFile $outFile

API response is not showing in Powershell

I'm using Powershell v4.
I am calling a POST API and want to see the response when I run Invoke-RestMethod but it just shows a new line.
How can I output the response from the API to the console? I have tried running it in a script with Write-Host and Write-Output, but nothing appears on the console screen.
PowerShell script:
$Response = Invoke-RestMethod -Uri https://localhost:8081/myAPI -Body '{"username":"xyz","password":"xyz"}' -ContentType application/json -Method POST
Write-Host $Response
I can see the response when I curl the API but cannot see it in my PowerShell script which I need to use.
CURL
curl -H "Content-Type: application/json" -X POST -d '{"username":"xyz","password":"xyz"}' https://localhost:8081/myAPI
It looks like its not even hitting the API as nothing appears in the API logs when triggering from Powershell. Not sure how to resolve that if that is the problem.
Your command has a syntax error. Your contenttype is not in proper format.
You have:
-ContentType application/json
Change it to:
-ContentType "application/json"