A
A
Andrey Strelkov2020-11-12 13:38:01
PowerShell
Andrey Strelkov, 2020-11-12 13:38:01

How to download a file using Powershell from a site with authorization?

Good afternoon, there is a specific site, for example mysite.ru, upon entering which an authorization window pops up (that is, not by means of the site code, but at the level of the web server, it seems that the browser throws out the authorization window, the standard authorization seems to be basic)
The problem is that from this site, from a certain path, you need to download the file using Powershell, which is located at mysite.ru/download/somefile.txt.
But everything seems to be simple

Invoke-WebRequest -Uri "http://mysite.ru/download/somefile.txt" -Credential $credentials


However, if you simply go to this link in the browser, then there is no mandatory authorization window, and the web server simply returns 404, but if you go to mysite.ru first, log in, and then follow this link, then the file is successfully downloaded

. It seems that you need to first log in to mysite.ru in powershell, and then somehow download from the link mysite.ru/download/somefile.txt (i.e. as if you need to somehow open and hold the session)

How can I solve the problem , Tell me please

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
azarij, 2020-11-12
@strelkov_av

in chrome dev tools, you intercept all requests to the site during authorization and downloading the file. devtools will even copy requests in powershell format for you.
repeat them in powershell saving the session, as MaxKozlov pointed out , via -sessionvariable/-websession.
PROFIT!!!

M
MaxKozlov, 2020-11-12
@MaxKozlov

Sessions, apparently
A couple of requests + parameters -SessionVariable / -WebSession

A
Andrey Strelkov, 2020-11-13
@strelkov_av

Mmm, do I understand correctly, on the example of the site: sbfactory.ru/cdfiles
1) I open the browser, the record is enabled by default in devtoolz
2) I insert sbfactory.ru/cdfiles
into the address 3) An authorization window pops up where I enter (cdcustomer/allowmetodownload)
4) Authorization is successful
5) Next, I click on the downloaded file sbfactory.ru/cdfiles/cd_install.exe
And at the output I have roughly 3 requests in devtools (I ignore the 4th with favicon)
I click on any in the context, select Copy all Powershell
At the output I get:

Invoke-WebRequest -Uri "http://sbfactory.ru/cdfiles" session -Headers @{
"Pragma"="no-cache"
  "Cache-Control"="no-cache"
  "Authorization"="Basic Y2RjdXN0b21lcjphbGxvd21ldG9kb3dubG9hZA=="
  "Upgrade-Insecure-Requests"="1"
  "User-Agent"="Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36"
  "Accept"="text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
  "Accept-Encoding"="gzip, deflate"
  "Accept-Language"="ru"
}

Invoke-WebRequest -Uri "http://sbfactory.ru/cdfiles/" -Headers @{
"Pragma"="no-cache"
  "Cache-Control"="no-cache"
  "Authorization"="Basic Y2RjdXN0b21lcjphbGxvd21ldG9kb3dubG9hZA=="
  "Upgrade-Insecure-Requests"="1"
  "User-Agent"="Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36"
  "Accept"="text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
  "Accept-Encoding"="gzip, deflate"
  "Accept-Language"="ru"
}

Invoke-WebRequest -Uri "http://sbfactory.ru/cdfiles/cd_install.exe" -Headers @{
"Pragma"="no-cache"
  "Cache-Control"="no-cache"
  "Authorization"="Basic Y2RjdXN0b21lcjphbGxvd21ldG9kb3dubG9hZA=="
  "Upgrade-Insecure-Requests"="1"
  "User-Agent"="Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36"
  "Accept"="text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
  "Referer"="http://sbfactory.ru/cdfiles/"
  "Accept-Encoding"="gzip, deflate"
  "Accept-Language"="ru"
}

And somewhere between the lines, I should use something like
https://stackoverflow.com/a/51774034/13428313
i.e. like this
$Global:loginResponse = Invoke-WebRequest -uri ("https://dealer.md-bmc.rpdss.com/" + $form.Action) -SessionVariable login -Method post -Body $form.Fields

$fileUploadPage = Invoke-WebRequest -Uri $fileUploadurl -WebSession $login

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question