SpanningCloudApps / SB365-Powershell

Spanning Backup for Office 365 REST API PowerShell Module
https://spanning.com
Apache License 2.0
10 stars 5 forks source link

Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send. #34

Closed agrecaBAS closed 3 years ago

agrecaBAS commented 3 years ago

Receiving the following error when attempting to run any command after populating the Auth information in a variable.

Example: $spanningAuth = Get-SpanningAuthentication -ApiToken $adminAPIToken -Region "US" -AdminEmail $AdminUsername Get-SpanningTenantInfo -AuthInfo $spanningAuth

Error: Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send. At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\SpanningO365\4.0.0.0\Private\Invoke-SpanningRequest.ps1:178 char:20 ... $results = Invoke-WebRequest -uri $request -Headers $headers -Method ...


     CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
     FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
agrecaBAS commented 3 years ago

This issue is resolved by adding the following to the script.

SPMatthewMcD commented 3 years ago

I am not having this issue. In case others do, where did you add the TLS version?

Thanks in advance.

agrecaBAS commented 3 years ago

Adding [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 before the invocation commands is what resolved the issue. In my case, I added it near or at the top of the script.

SPMatthewMcD commented 3 years ago

Thanks! That changes it for the session. On my development machine it's using TLS 1.2 by default. So my script doesn't need this. Though I did find the following bit of conditional code: `try { if ([Net.ServicePointManager]::SecurityProtocol -notcontains 'Tls12') {

}

}`

Mine returns SystemDefault so this forces Tls12 even though it does not appear to need it. After running the code I get Tls12.

Thank you for the clarification