PowerShell Invoke RestMethod response as file to PDF - powershell

I am using invoke-restmethod GET in Powershell which produces a PDF file as a response.
I can see that file as an encoded format in the PowerShell window.
I need to create a PDF file from the response.
How to achieve?

Use the -OutFile parameter:
Invoke-RestMethod $uri -Method Get -OutFile output.pdf

Related

PowerShell - downloads TXT file with WRONG content using the REST API

I have a file which I download using the REST API.
Just to emphasize that I tried with both commands: Invoke-RestMethod and Invoke-WebRequest
$uri = "https://bitbucket.org.dev/projects/TEST/repos/fa/browse/Packages/ATS.txt"
$API_KEY="ZTU2MT"
Invoke-WebRequest -Headers #{Authorization=$("Basic {0}" -f $API_KEY)} -Uri $uri -OutFile ATS.txt
If I access that URI in browser file or download it manually file can be viewd without any issue in clear way.
This is the content of the file (its begining)
#
# exported extension module.
# source ""
# timestamp (utc) "2020-03-30 12:06:23.3"
# ***** DO NOT EDIT! *****
But download file looks completely different (like it is in HTML format)
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta http-equiv="X-..."
Also I have some zip file which needs to be downloaded as well, but with it also I am getting invalid file which cannot be extracted (opened)
As written in MSDN, Invoke-WebRequest returns an object of type BasicHtmlWebResponseObject.
You need to select the Content property to get the text you are looking for.
Invoke-WebRequest -Headers #{Authorization=$("Basic {0}" -f $API_KEY)} -Uri $uri | Select-Object -Expand Content | Out-File -FilePath c:\somefile.txt
Update:
Check this post to find more info about downloading files from a private repository in BitBucket.
https://stackoverflow.com/a/50222671/13440610

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

Downloading a Report Viewer report to PDF with Powershell

I'm trying to call a report server report with parameters and save it as PDF. It works fine in a browser. The PDF file is saved, but I can't choose where to save it.
I'm trying to use Powershell to archieve it.
$URI = "http://servername/ReportServer/Pages/ReportViewer.aspx?%2fReportname%2fTemp&rs:Format=PDF&Guid=parameter1&StartDate=01-02-2018&StartTime=07:00&EndDate=01-02-2018&EndTime=17:00"
$Outputfile = "C:\temp\test.pdf"
(Invoke-WebRequest -Uri $URI -OutFile $Outputfile -UseDefaultCredentials -TimeoutSec 30)
The Powershell code downloads a PDF with the correct layout, but no data. It says "No data available". Any ideas?

Invoke-WebRequest pass url as script argument (PowerShell)

I am trying to download file from the web using following command
Invoke-WebRequest $url -OutFile $filePath -Headers $Headers
I have argument, which contains this url and it is passed as parameter
[string]$artifactHttpAddress = $args[2]
Currently its value is
http://10.45.48.26/httpAuth/repository/downloadAll/TeamCityTest_Build/529:id/artifacts.zip
So, when I try to invoke WebRequest with following command
Invoke-WebRequest $artifactHttpAddress -OutFile c:/test.zip -Headers $Headers
it is downloading empty zip file .
but when I try to assign this url to the variable and invoke web request
$url = "http://10.45.48.26/httpAuth/repository/downloadAll/TeamCityTest_Build/529:id/artifacts.zip"
Invoke-WebRequest $url -OutFile c:/test.zip -Headers $Headers
It is working correctly, downloads zip file, which have some content in it.
I tried following script
Write-Host([string]$url -eq [string]$artifactHttpAddress)
Write-Host([string]$url)
Write-Host([string]$artifactHttpAddress)
It outputs
False
http://10.45.48.26/httpAuth/repository/downloadAll/TeamCityTest_Build/528:id/artifacts.zip
http://10.45.48.26/httpAuth/repository/downloadAll/TeamCityTest_Build/531:id/artifacts.zip
What is happening and why?
p.s. this script is inside ScriptBlock
It looks to me, based on your output, that $url and $artifactHttpAddress are not the same value. Does the ZIP file exist at the URL with 531 in it?

Invoke-WebRequest error when running ps1 script

I am trying to write a ps 3.0 script that will post a list of 200 words to a web server. After I have prepared the list, I use:
Invoke-WebRequest -Uri $url -Method POST -Body $PostParams -OutFile C:...\upload_log.tmp.txt
When I run this script from the ISE editor it works fine. However, when I run it from outside the editor (cmd, ps shell, task scheduler) I get this error message:
Invoke-WebRequest : You must write ContentLength bytes to the request stream
Any suggestions?