Question:
I want try transfer my bash script from linux to powershell but cant understand why it failed.
Linux command:
1 2 |
wget -q -x --user-agent="blablabla" --keep-session-cookies --load-cookies cook.txt http://site.com/qqq |
powershell code:
1 2 3 4 5 6 |
$source = "http://site.com/qqq" $destination = "d:\site\qqq" $wc = New-Object System.Net.WebClient $wc.DownloadFile($source, $destination) |
but this code only download page without cookies. And i cant find how i can send PHPSESSID to site.
Please explain to me how to do it.
Answer:
Ok so you have two options.
- Run wget on Windows.
- Set properties on the webclient object to
replicate the functionality set by the wget flags.
The first one is easy, just download the Windows version of wget.
Here is the code for the second one.
1 2 3 4 5 6 7 8 9 10 |
$source = "http://site.com/qqq" $destination = "d:\site\qqq" $wc = New-Object System.Net.WebClient # Single Example $wc.Headers.Add([System.Net.HttpRequestHeader]::Cookie, "name=value") # Multi Example $wc.Headers.Add([System.Net.HttpRequestHeader]::Cookie, "name=value; name2=value2"); $wc.DownloadFile($source, $destination) |
Look inside your cookies.txt file to get the name value pairs.
I’m not sure how to replicate the -x
functionality of wget
. Give the above a try and see what it does with the file after downloading.
Note – I can’t test this…