title: When using PowerShell 'Invoke-WebRequest' with SSL, how can the 'session' be deleted on the client side after disconnect?
body: <p>Good morning,</p>
  
  <p>I am using PowerShell to interact with the VMWare vCloud API and am having problems after disconnecting from vCloud.</p>
  
  <p>The process for connecting and using the API is as follows:</p>
  
  <ul>
  <li>Connect to vCloud using a POST</li>
  <li>Perform necessary operations</li>
  <li>Disconnect using DELETE (This clears down the session at the remote endpoint)</li>
  </ul>
  
  <p>All communication is over HTTPS.</p>
  
  <p>The problem is that when this process run for the first in a new PowerShell console everything is OK.  However if I want to repeat this process again I invariably get the following message:</p>
  
  <pre><code>Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred     on a send.                            
  At C:\tfs\poshvcloud\solutions\poshvcloud\functions\helpers\_Invoke- vCloudRequest.ps1:95 char:17                             
  +     $response = Invoke-WebRequest @splat                                                                                   
  +                 ~~~~~~~~~~~~~~~~~~~~~~~~                                                                                   
     + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException 
     + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand            
  </code></pre>
  
  <p>I have used Fiddler in an attempt to find out what is going on, and interestingly I only ever see 'CONNECT' operations and no attempt to even communicate with vCloud when the above error is displayed.</p>
  
  <p>Indeed I have checked the ServiceEndPoint using <code>[System.Net.ServicePointManager]::FindServicePoint("https://vcloud.example.com/")</code> and I get an object back and it states I do not have any connections, but for some reason the connection is not re-established when I try to connect again.  It is as if PowerShell does not bother with SSL handshake again.</p>
  
  <pre><code>BindIPEndPointDelegate :                                                              
  ConnectionLeaseTimeout : -1                                                           
  Address                : https://vcloud.example.com/                
  MaxIdleTime            : 100000                                                       
  UseNagleAlgorithm      : True                                                         
  ReceiveBufferSize      : -1                                                           
  Expect100Continue      : False                                                        
  IdleSince              : 18/07/2014 09:11:34                                          
  ProtocolVersion        : 1.1                                                          
  ConnectionName         : https                                                        
  ConnectionLimit        : 2                                                            
  CurrentConnections     : 0                                                            
  Certificate            : System.Security.Cryptography.X509Certificates.X509Certificate
  ClientCertificate      :                                                              
  SupportsPipelining     : True                                                         
  </code></pre>
  
  <p>One weird thing is that if I attempt to do another operation quickly enough it does go through, but then eventually times out and I get the error.</p>
  
  <p>The only way I can this to work again is to close down my current PowerShell console and start a new one and then load all the necessary modules.</p>
  
  <p>I have tried to 'DisableKeepAlive' on the Inovke-WebRequest but this causes the API calls to fail completely.  I then tried to just do it on the disconnect but this did not work either.</p>
  
  <p>I am thinking that I might need to write my own version Invoke-WebRequest, but I still think I am going to have to clear down things and I not sure how.  I would rather stick with Invoke-WebRequest if I can.</p>
  
  <p>I know that there is PowerCLI from VMWare that takes care of all of this, however that is a huge module and only a fraction of it is applicable to vCloud.  Indeed I started off using it, but due to some inconsistencies and a large dependency I have created my own vCloud module talking to the vCloud REST API directly.</p>
  
  <p>I hope this is enough information, but please let me know if more is required.  Any help is greatly appreciated.</p>
  
  <p>Kind regards, Russell</p>