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
4
  • 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; Commented Sep 15, 2022 at 3:22
  • Same concept with Powershell (see this answer). Where are you getting hung up at? Please post the code for your Invoke-WebRequest attempt Commented Sep 15, 2022 at 4: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 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 website.com/my-subscriptions/usn/… Am I wrong about how curl handles cookies? The command completes ok. Commented Sep 15, 2022 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 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? Commented Sep 15, 2022 at 23:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.