title: Using System.Net.WebClient in PowerShell to access a HTTPS URL with SAN Certificate
body: <p>Bit of an odd problem here. The script we're using is:</p>
  
  <pre><code>$username = "itrpo"
  $url = "https://homepages.domain.com/cgi-bin/checkperms.cgi?user=$username"
  $homepagesUser = "REMOVED"
  $homepagesPass = "REMOVED"
  $webclient = New-Object System.Net.WebClient
  $webclient.Credentials = new-object System.Net.NetworkCredential($homepagesUser, $homepagesPass)
  $result = $webclient.DownloadString($url)
  </code></pre>
  
  <p>When running this from my desktop (Windows 8, PowerShell v3 using .NET v4.0.30319) it works.
  When running this from a server (Windows Server 2008 R2, PowerShell v2 using .NET v2.0.50727) it does not. Ultimately it hangs and returns the error: Exception calling "DownloadString" with "1" argument(s): "The operation has timed out"</p>
  
  <p>Using NetMon I managed to determine that on the server it was failing to perform any TLS client key exchange. </p>
  
  <p>The SSL certificate for homepages.domain.com is actually a SAN certificate, with homepages being an alias for the actual server name (let's call it servera.domain.com). When I changed $url in the script to use the actual server name as so:</p>
  
  <pre><code>$url = "https://servera.domain.com/cgi-bin/checkperms.cgi?user=$username"
  </code></pre>
  
  <p>... it worked, but this is not ideal (if the service is moved to another machine the script will break). </p>
  
  <p>It seems as though PowerShell v2 (.NET v2) is having problems with the SAN certificate? Further evidence to support this is that we use the above style script against another server with a standard SSL certificate it communicates fine.</p>
  
  <p>Could it be due to differences in the WebClient class being used? Here is the documentation for it under .NET v2:
  <a href="http://msdn.microsoft.com/en-us/library/system.net.webclient%28v=vs.80%29.aspx">http://msdn.microsoft.com/en-us/library/system.net.webclient%28v=vs.80%29.aspx</a>
  and the documentation for it under .NET v4:
  <a href="http://msdn.microsoft.com/en-us/library/system.net.webclient%28v=vs.100%29.aspx">http://msdn.microsoft.com/en-us/library/system.net.webclient%28v=vs.100%29.aspx</a></p>
  
  <p>I don't know enough about programming to really interpret the above documentation properly (or know if I'm on the right track).</p>
  
  <p>Ideally we want that script to function under PowerShell v2 and .NET v2 without having to upgrade things or force PowerShell to use .NET v4. If my suspicion is correct is there is parameter we can specify / a way to modify the script to "fix" this issue?</p>
  
  <p>Many thanks for any advice.</p>