Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Invoke-WebRequest to download in-browser pdf

$
0
0

Hello,

I'm trying to figure out how to download a pdf from a url. I've tried the following code, however the file is always corrupt.  When I put the url in a browser, it shows the pdf inline with the option to save as a .pdf.  I'm not sure how to mirror the save action and working with web pages in PowerShell is new to me.  I've noticed that there seems to be a redirect url for login authorization ($redirectLogin) but I'm not certain how to use that (or if it's needed). 

$LoginURL = 'https://login.procore.com'
$redirectLogin = "https://app.procore.com/auth/procore"
$downloadURL = 'https://app.procore.com/352325/project/checklists/lists/1771201.pdf'
$outputLoc = "$PSScriptRoot\Test.pdf"
$WebResponse = Invoke-WebRequest $LoginURL -SessionVariable 'session'

$LoginForm = $WebResponse.Forms[0]
$LoginForm.Fields["utf8"] = $LoginForm.Fields["utf8"]
$LoginForm.Fields["authenticity_token"] = $LoginForm.Fields["authenticity_token"]
$LoginForm.Fields["session_sso_target_url"] = $LoginForm.Fields["session_sso_target_url"]
$LoginForm.Fields["session_email"] = $user
$LoginForm.Fields["session_password"] = $pass
$LoginForm.Fields["session_remember_me"] = $false
$Url = $LoginURL+$LoginForm.Action

#Wasn't sure if I can call the site once with the credentials and then call without

$WebResponse = Invoke-WebRequest -Uri $url -Method Post -Body $LoginForm -SessionVariable $session

$WebResponse = Invoke-WebRequest -Uri $downloadURL -Credential Get-Credential -OutFile $outputLoc

Any pointers?


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>