Answer the question
In order to leave comments, you need to log in
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
Answer the question
In order to leave comments, you need to log in
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!!!
Sessions, apparently
A couple of requests + parameters -SessionVariable / -WebSession
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"
}
$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 questionAsk a Question
731 491 924 answers to any question