title: Powershell to Download SSRS Report
body: <p>We're using SSRS to generate database reports that publish to an internally hosted webserver.  I'm then writing a PowerShell script that periodically runs and downloads the reports as PDFs.</p>
  
  <p>I've gotten it to work running as an admin.  But for security purposes, the automated task must run as a generic domain service account.</p>
  
  <p>The code I use to download the PDF using my account is:</p>
  
  <pre><code> $webClient = New-Object System.Net.WebClient
   $webClient.UseDefaultCredentials = $TRUE
  
   $webClient.DownloadFile($ssrsURL, $filePath)
  </code></pre>
  
  <p>When I run this code as the service account, I get an 500 Internal Server error.  I've tried replacing the second line of code with:</p>
  
  <pre><code>$webClient.Credentials = New-Object System.Net.NetworkCredential("Username", "Password", "Domain");
  </code></pre>
  
  <p>When I try this, I get a 401 Unauthorized error.  Even though the Username / Password I put in the code is my own, and works when I log in to the webserver via IE.</p>
  
  <p>The answer may be in my code, domain / local security policies, or SSRS security settings...</p>
  
  <p>Some questions that might be in the right direction are:</p>
  
  <ol>
  <li>What other means of authentication could I try in PowerShell</li>
  <li>Does anyone know of any IIS / SSRS settings I could try altering?</li>
  <li>What domain or local security policies are allowing "UseDefaultCredentials" to work when run under my account, but not the service account set up?</li>
  </ol>
  
  <p>Thanks in advance for any ideas.</p>