Hi,
I have a question about Invoke-WebRequest cmdlet. I’m trying to use it to login into a website, and download a report (Excel file).
To login I’m using POST method, and I don’t have a problem with this part, but in the next step when I’m trying to download the file, instead of getting my data, the excel file contains login page…
Any idea where I’m making a mistake?
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $reportxlsx = "C:\My Files\report.xlsx" $r = Invoke-WebRequest -Uri https://www.mywebsite.com/ -SessionVariable st $form = $r.Forms[0] $form.Fields["USER"]="username" $form.Fields["USERID"]="userid" $form.fields["PASSWORD"]="mypassword" $form.fields["GROUP"]="mygroup" $form.Fields["smagentname"]="randomstring" $form.Fields["smauthreason"]="0" $form.Fields["stopAutoFill"]="" $form.Fields["target"]="https://www.mywebsite.com/folderwithreport" $form.Fields $r = Invoke-WebRequest -Uri ("https://www.mywebsite/forms/login.fcc" + $form.Action) -WebSession $st -Method Post -Body $form.Fields -OutFile $Text $r = Invoke-WebRequest -Uri ("https://www.mywebsite.com/repository/iB4CCFF7457124F1389C589D9470521D9") -WebSession $st -Method Get -OutFile $reportxlsx