Get excel file from a web portal

Hi,

We have a portal which has link to download excel report, I need a way to download this server report using powershell. I did invoke-webrequest and parsed through the output to the form which contains the information I need as below;
<p lang=“en-US” style=“margin: 0in; font-family: Calibri; font-size: 11.0pt;”>PS C:&gt; $portal= Invoke-WebRequest -Uri “http://xxxx.srv.xxxx.com/xxdb/server_results.asp?devicename=server148”</p>
<p lang=“en-US” style=“margin: 0in; font-family: Calibri; font-size: 11.0pt;”>PS C:&gt; $portal.Forms[1]</p>
<p lang=“en-US” style=“margin: 0in; font-family: Calibri; font-size: 11.0pt;”>Method - Get</p>
<p lang=“en-US”>Action - server_results_excelexport.asp</p>
<p lang=“en-US”>Fields - {[devicename, server148]}</p>

Use -Output parameter with the direct link to the file.

Invoke-WebRequest -Uri http://someserver/files/Summary.xlsx -Output c:\temp\report.xlsx

Hi Prasoon,

I found the Action ‘server_results_excelexport.asp’ generates the excel report. Is there a way to run that asp from my powershell session so that I can then get that excel report?

Thanks,

Kishor

 

if you have an option to download via a browser, you can right click the download button to get the direct link.

Hi Prasoon,

I got the direct link and I am able to download using browser with it; ‘http://xxxx.srv.xxxx.com/xxdb/server_results_excelexport.asp?devicename=server148

$url = “http://xxxx.srv.xxxx.com/xxdb/server_results_excelexport.asp?devicename=server148

When I do $file = invoke-webrequest -uri $url -method get

I get below and if I use -outfile property I get a file but excel is not able to open it, says corrupt;

PS H:&gt; $file.RawContent
HTTP/1.1 200 OK
Content-Disposition: attachment; filename=server148_export_20181114_075201.xls
Persistent-Auth: true
Content-Length: 62153
Cache-Control: private
Content-Type: application/vnd.ms-excel
Date: Wed, 14 Nov 2018 06:52:01 GMT
Set-Cookie: ASPSESSIONIDAQBTBSTQ=KBKBDCHDKKDJJPHNDAEFCOJO; path=/
Server: Microsoft-IIS/8.0
X-Powered-By: ASP.NET

It should be directly pointing to a file I believe,

see example https://stackoverflow.com/questions/22447326/powershell-download-image-from-an-image-url

Hi Prasoon,

Below statement solved my problem;

[System.IO.File]::WriteAllBytes("$output", $file.content)

I found solution on website;

https://www.reddit.com/r/PowerShell/comments/30d8uu/downloading_a_file_from_a_redirected_link_using/

Regards,

Kishor

Thanks for sharing ! :slightly_smiling_face: