title: SSRS and PowerShell: Get report as Excel
body: <p>I'm trying to make PowerShell send a web request to our SSRS server and capture the results. I've hit a wall using the <code>rs:FORMAT=EXCEL</code> parameter in the SSRS url string. I have the following:</p>
  
  <p>First, init the credentials:</p>
  
  <pre><code>$User = "MYDOMAIN\MyUser"
  $PWord = ConvertTo-SecureString -String "WooHooStringP$W0rd" -AsPlainText -Force
  $c = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $User, $PWord
  </code></pre>
  
  <p>Now, request a report:</p>
  
  <pre><code>Invoke-WebRequest `
  -UserAgent ([Microsoft.PowerShell.Commands.PSUserAgent]::InternetExplorer) `
  -Credential $c `
  -Uri "http://myserver/ReportServer_DEV/Pages/ReportViewer.aspx?/folder+path/report+name"
  </code></pre>
  
  <p>This works fine. I can even grab the results (enclosing this request and using ().Content).
  Then, specify a format instead of plain rendering:</p>
  
  <pre><code>Invoke-WebRequest `
  -UserAgent ([Microsoft.PowerShell.Commands.PSUserAgent]::InternetExplorer) `
  -Credential $c `
  -Uri "http://myserver/ReportServer_DEV/Pages/ReportViewer.aspx?/folder+path/report+name&amp;rs:format=HTML4.0"
  </code></pre>
  
  <p>Note the <code>rs:Format</code> specification? Works like a charm.</p>
  
  <p>Then, for the grande finale, give me an Excel file:</p>
  
  <pre><code>Invoke-WebRequest `
  -UserAgent ([Microsoft.PowerShell.Commands.PSUserAgent]::InternetExplorer) `
  -Credential $c `
  -Uri "http://myserver/ReportServer_DEV/Pages/ReportViewer.aspx?/folder+path/report+name&amp;rs:format=EXCEL"
  </code></pre>
  
  <p>No can do, bud:</p>
  
  <pre><code>Invoke-WebRequest : The remote server returned an error: (401) Unauthorized.
  At line:1 char:11
  + $bytez = (Invoke-WebRequest `
  +           ~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
      + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
  </code></pre>
  
  <p>Why does the <code>rs:format=EXCEL</code> option throw an Unauthorised exception where all the other URLs are served by SSRS?</p>