title: Powershell Invoke-RestMethod over HTTPS
body: <p>I'm trying to communicate with a service over powershell but I am failing miserably. I suspect it is the certificate and I have googled for the answer and found two options, none of which worked for me. I have also tried to combine the two unsuccessfully.</p>
  
  <p><strong>Option 1:</strong></p>
  
  <pre><code>add-type @"
      using System.Net;
      using System.Security.Cryptography.X509Certificates;
      public class TrustAllCertsPolicy : ICertificatePolicy {
          public bool CheckValidationResult(
              ServicePoint srvPoint, X509Certificate certificate,
              WebRequest request, int certificateProblem) {
              return true;
          }
      }
  "@
  [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
  
  $urlJSON = "https://internal.ad.local/path/api_jsonrpc.php"
  
  #Create authentication JSON object using ConvertTo-JSON
  $objAuth = (New-Object PSObject | Add-Member -PassThru NoteProperty jsonrpc '2.0' |
  Add-Member -PassThru NoteProperty method 'user.authenticate' |
  Add-Member -PassThru NoteProperty params @{user="user";password="password"} |
  Add-Member -PassThru NoteProperty id '2') | ConvertTo-Json
  
  
  Invoke-RestMethod -Uri $urlJSON -body $objAuth -method "Post"
  </code></pre>
  
  <p><strong>Option 2:</strong></p>
  
  <pre><code>[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
  
  $urlJSON = "https://internal.ad.local/path/api_jsonrpc.php"
  
  #Create authentication JSON object using ConvertTo-JSON
  $objAuth = (New-Object PSObject | Add-Member -PassThru NoteProperty jsonrpc '2.0' |
  Add-Member -PassThru NoteProperty method 'user.authenticate' |
  Add-Member -PassThru NoteProperty params @{user="user";password="password"} |
  Add-Member -PassThru NoteProperty id '2') | ConvertTo-Json
  
  
  Invoke-RestMethod -Uri $urlJSON -body $objAuth -method "Post"
  </code></pre>
  
  <p>Here's the error message:</p>
  
  <pre><code>Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send.
  At C:\Users\user\AppData\Local\Temp\46eaa6f7-62a0-4c10-88d1-79212d652bc9.ps1:24 char:1
  + Invoke-RestMethod -Uri $urlJSON -body $objAuth -method "Post"
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
      + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
  </code></pre>
  
  <p>I might add:</p>
  
  <ul>
  <li>surfing directly to the service works with a web browser</li>
  <li>I tried opening up to HTTP as well, and that worked</li>
  <li>The certificate used by the service is self-signed but trusted by my machine via a root certficate (no warnings is issues in IE or Chrome)</li>
  <li>I have done network captures and made sure that a packets does indeed reach the server.</li>
  </ul>
  
  <p>Any suggestions appreciated!</p>
  
  <p>Kind regards,
  Patrik</p>
  
  <p>Updated post as to suggestions made by Mr Tree below:</p>
  
  <pre><code>Name                       : lambda_method
  DeclaringType              :
  ReflectedType              :
  Module                     : RefEmit_InMemoryManifestModule
  MethodHandle               :
  Attributes                 : PrivateScope, Public, Static
  CallingConvention          : Standard
  IsSecurityCritical         : False
  IsSecuritySafeCritical     : False
  IsSecurityTransparent      : True
  ReturnType                 : System.Boolean
  ReturnParameter            :
  ReturnTypeCustomAttributes : System.Reflection.Emit.DynamicMethod+RTDynamicMethod+EmptyCAHolder
  MemberType                 : Method
  MethodImplementationFlags  : NoInlining
  IsGenericMethodDefinition  : False
  ContainsGenericParameters  : False
  IsGenericMethod            : False
  IsPublic                   : True
  IsPrivate                  : False
  IsFamily                   : False
  IsAssembly                 : False
  IsFamilyAndAssembly        : False
  IsFamilyOrAssembly         : False
  IsStatic                   : True
  IsFinal                    : False
  IsVirtual                  : False
  IsHideBySig                : False
  IsAbstract                 : False
  IsSpecialName              : False
  IsConstructor              : False
  CustomAttributes           :
  MetadataToken              :
  </code></pre>
  
  <p>Update 2 based on a comment by Mr Tree:</p>
  
  <pre><code>Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send.
  At C:\Users\user\AppData\Local\Temp\ff47910e-fd8e-4be8-9241-99322144976a.ps1:13 char:1
  + Invoke-RestMethod -Uri $urlJSON -body $objAuth -method "Post"
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
      + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
  </code></pre>