Forum Discussion

Patrick0411's avatar
Patrick0411
Copper Contributor
Aug 07, 2024
Solved

Exchange Online Powershell not connecting in scheduled Task script

Hello everyone,

 

I am having this issue where I can't find any useful answer.

We are connecting to Exchange Online with certificate based authentication and it works fine when we run the script manually.

Running the script via scheduled task with the same user, we always receive the following error messages.

 

PS>TerminatingError(Get-ConnectionContext): "One or more errors occurred."
PS>TerminatingError(): "An error occurred while sending the request."

An error occurred while sending the request.
At C:\Program Files\WindowsPowerShell\Modules\ExchangeOnlineManagement\3.5.0\netFramework\Exchange
OnlineManagement.psm1:762 char:21
+                     throw $_.Exception.InnerException;
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], HttpRequestException
    + FullyQualifiedErrorId : An error occurred while sending the request.

 

This is the command we use to connect:

 

Connect-ExchangeOnline -AppId $AppId -CertificateThumbprint $CertificateThumbprint -Organization $Organization -ShowBanner:$false -loglevel all

 

Has anyone experienced this before?

 

ExchangeOnlineManagement Module is Version 3.5.0

Powershell Version is 5.1.14393.7155 

  • It took me long time, but after creating a seperate scheduled script (only for testing Exchange Online connection), I finally found the issue.
    The new script worked fine, so it had to be something about my old script.

    As it turned out, we had a Webserver API Call executing before the ExO connect in our script and I assume the connection never closed correctly. After I removed / moved this API call, the script is working fine.
    Maybe it was blocking the same socket, which the script uses to connect to Exchange?

    Probably not really useful for you, but I hope it helps anyway.

8 Replies

  • TristanDev's avatar
    TristanDev
    Copper Contributor

    Hi,

     

    When I had this problem, I added this command to the beginning of my script to connect.

    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12


    After a while, I no longer needed this line forcing the TLS. But I can’t remember what was done to correct that behaviour.

     

     

  • Patrick0411 Currently, I do not see this behaviour in other environments.

     

    Which operating system is the script running?
    Are the TLS settings for .NET Framework on that system set to support TLS 1.2?

    -Thomas 

    • Patrick0411's avatar
      Patrick0411
      Copper Contributor

      ThomasStensitzki-MVP thank you for your help.

      We are running this on Windows Server 2016 Datacenter.

      TLS 1.2 client is enabled.

       

      Here you can see all the tls settings:

      Path                                                                                       Name                         Value
      ----                                                                                       ----                         -----
      HKLM:\SOFTWARE\Microsoft\.NETFramework\v4.0.30319                                          SystemDefaultTlsVersions         1
      HKLM:\SOFTWARE\Microsoft\.NETFramework\v4.0.30319                                          SchUseStrongCrypto               1
      HKLM:\SOFTWARE\WOW6432Node\Microsoft\.NETFramework\v4.0.30319                              SystemDefaultTlsVersions         1
      HKLM:\SOFTWARE\WOW6432Node\Microsoft\.NETFramework\v4.0.30319                              SchUseStrongCrypto               1
      HKLM:\SOFTWARE\Microsoft\.NETFramework\v2.0.50727                                          SystemDefaultTlsVersions         1
      HKLM:\SOFTWARE\Microsoft\.NETFramework\v2.0.50727                                          SchUseStrongCrypto               1
      HKLM:\SOFTWARE\WOW6432Node\Microsoft\.NETFramework\v2.0.50727                              SystemDefaultTlsVersions         1
      HKLM:\SOFTWARE\WOW6432Node\Microsoft\.NETFramework\v2.0.50727                              SchUseStrongCrypto               1
      HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.3\Server Enabled                  Not Found
      HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.3\Server DisabledByDefault        Not Found
      HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.3\Client Enabled                  Not Found
      HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.3\Client DisabledByDefault        Not Found
      HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Server Enabled                          1
      HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Server DisabledByDefault                0
      HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client Enabled                          1
      HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client DisabledByDefault                0
      HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.1\Server Enabled                          0
      HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.1\Server DisabledByDefault                1
      HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.1\Client Enabled                          0
      HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.1\Client DisabledByDefault                1
      HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.0\Server Enabled                          0
      HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.0\Server DisabledByDefault                1
      HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.0\Client Enabled                          0
      HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.0\Client DisabledByDefault                1

       

      • Can you login to Exchange Online PowerShell using another account?
        And can you check if the use of Remote PowerShell is enabled for the account having the issues?

        Get-User -Identity <UserIdentity> | Format-List RemotePowerShellEnabled
        -Thomas

Resources