EWS Exchange Error – the underlying connection was closed an unexpected error occurred on a send

On trying to run a EWS Script the follow error came up

The request failed. The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

The addition to of the following line above to silenced the problem but did not fix the communication. Instead brought up an new error

“The request failed. The underlying connection was closed: An unexpected error occurred on a send.”

First you can add this line after each of the errors to give you the full error
$error[0] | fl -force

The full error displayed :

System.Management.Automation.PSInvalidOperationException: There is no Runspace available t  scripts in this thread. self sign certificate

The below code needs to be added instead to properly ignore the self signed certificate issues:

## Code From http://poshcode.org/624
## Create a compilation environment
$Provider=New-Object Microsoft.CSharp.CSharpCodeProvider
$Compiler=$Provider.CreateCompiler()
$Params=New-Object System.CodeDom.Compiler.CompilerParameters
$Params.GenerateExecutable=$False
$Params.GenerateInMemory=$True
$Params.IncludeDebugInformation=$False
$Params.ReferencedAssemblies.Add("System.DLL") | Out-Null
 
$TASource=@'
  namespace Local.ToolkitExtensions.Net.CertificatePolicy{
    public class TrustAll : System.Net.ICertificatePolicy {
      public TrustAll() { 
      }
      public bool CheckValidationResult(System.Net.ServicePoint sp,
        System.Security.Cryptography.X509Certificates.X509Certificate cert, 
        System.Net.WebRequest req, int problem) {
        return true;
      }
    }
  }
'@ 
$TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)
$TAAssembly=$TAResults.CompiledAssembly
 
## We now create an instance of the TrustAll and attach it to the ServicePointManager
$TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
[System.Net.ServicePointManager]::CertificatePolicy=$TrustAll
 
## end code from http://poshcode.org/624
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: -1.00 out of 5)
Loading...