0

I'm trying to convert a relatively simple bash script to run in Windows. It uses wget to download a file from a site that requires a login name but no password. I have been bashing my head against invoke-webrequest in Powershell for some time.

Working script, lightly obfuscated:

Login and grab the auth cookies & link

wget --save-cookies /tmp/cookie.txt --keep-session-cookies --post-data 'login=myusername' -O /tmp/session.output https://website.com/session

Parse out the link and use the cookies to grab the digest

wget --load-cookies /tmp/cookie.txt -O "/home/user/filename$(date "+%Y-%m-%d").pdf"  "https://website.com"
grep -o -E '/my-subscriptions/nnn/([^"#]+)pdf' /tmp/session.output

Clean up

rm /tmp/cookie.txt
rm /tmp/session.output
Edgar Magallon
  • 556
  • 2
  • 7
  • 15
SKaye
  • 21
  • 5
  • Here is a rough conversion to PS of some old C# code that uses cookies - **not working code**. `using namespace System.Net;[CookieContainer]$cookieJar = [CookieContainer]::new();$strURL = 'https://website.com/session';[string]$strResult = "";[HttpWebRequest]$hwRequest = [HttpWebRequest]([WebRequest]::Create($strURL));$hwRequest.CookieContainer = $CookieJar;$hwRequest.KeepAlive = $true;[HttpWebResponse]$hwResponse = [HttpWebResponse]$hwRequest.GetResponse();[StreamReader]$sr = [StreamReader]::new($hwResponse.GetResponseStream());$strResult = $sr.ReadToEnd();$sr.Close();return $strResult;` – Darin Sep 15 '22 at 03:22
  • Same concept with Powershell ([see this answer](https://stackoverflow.com/a/51225744/11954025)). Where are you getting hung up at? Please post the code for your Invoke-WebRequest attempt – Daniel Sep 15 '22 at 04:52
  • I got mad at Invoke-Webrequest, now I'm trying with Windows curl command (not curl in PS which is an alias for Invoke-Webrequest). I think the first statement is: curl https://website.com/session --data "login=mylogin" -L -c c:\temp\cookies.txt -o "c:\Temp\session.txt" But when I try to retrieve the page I get a size 0 file (forget the regex for now): curl -c c:\temp\cookies.txt -o c:\temp\myfilename.pdf https://website.com/my-subscriptions/usn/dfa30360-16db-013b-e99c-327f1c57592d.pdf Am I wrong about how curl handles cookies? The command completes ok. – SKaye Sep 15 '22 at 22:58
  • I'm almost there! -b to load cookies instead of -c. This works: curl -b c:\temp\cookies.txt -o c:\temp\nyt.pdf https://website.com/file.pdf Now I just need to figure out the grep to findstr translation. The original: `wget --load-cookies /tmp/cookie.txt -O "/tmp/myfile.pdf" "https://website.com\`grep -o -E '/my-subscriptions/usn/([^"#]+)pdf' /tmp/session.output\`"`; I don't think I need the regex since I haven't seen " or # in a filename yet. ...Admins, should this be a separate question now? – SKaye Sep 15 '22 at 23:44

0 Answers0