Get excel file from a web portal

Welcome Forums General PowerShell Q&A Get excel file from a web portal

This topic contains 7 replies, has 2 voices, and was last updated by

 
Participant
1 month ago.

  • Author
    Posts
  • #124422

    Participant
    Points: 57
    Rank: Member

    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;

    PS C:\> $portal= Invoke-WebRequest -Uri "http://xxxx.srv.xxxx.com/xxdb/server_results.asp?devicename=server148"

    PS C:\> $portal.Forms[1]

    Method – Get

    Action – server_results_excelexport.asp

    Fields – {[devicename, server148]}

  • #124457

    Participant
    Points: 884
    Helping Hand
    Rank: Major Contributor

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

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

      Participant
      Points: 57
      Rank: Member

      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

       

  • #124578

    Participant
    Points: 884
    Helping Hand
    Rank: Major Contributor

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

    • #124601

      Participant
      Points: 57
      Rank: Member

      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:\> $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

  • #124605

    Participant
    Points: 884
    Helping Hand
    Rank: Major Contributor

    It should be directly pointing to a file I believe,

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

  • #124629

    Participant
    Points: 57
    Rank: Member

    Hi Prasoon,

    Below statement solved my problem;

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

    I found solution on website;

    Downloading a file from a redirected link using Invoke-Webrequest from PowerShell

    Regards,

    Kishor

  • #124661

    Participant
    Points: 884
    Helping Hand
    Rank: Major Contributor

    Thanks for sharing ! 🙂

You must be logged in to reply to this topic.