# Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send while connecting Graph API

In the last few days, I’ve got two reports that my PowerShell module for Office 365 Health suddenly started giving errors. This was a bit weird because it worked perfectly fine on my end. But while I could understand one person having an issue of their own, with their network or firewalls, if the second person comes along with the same report, that means something else is going on.

blog

14 Dec 2019 en

![Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send while connecting Graph API](/wp-content/uploads/2019/12/img_5df4a8506b008.png)

[View images (2)](#pf-content-image-1-0)

In the last few days, I've got two reports that my **PowerShell** module for [**Office 365 Health**](https://github.com/EvotecIT/PSWinDocumentation.O365HealthService) suddenly started giving errors. This was a bit weird because it worked perfectly fine on my end. But while I could understand one person having an issue of their own, with their network or firewalls, if the second person comes along with the same report, that means something else is going on. The error is not telling much:

> Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send. At C:\Program Files\WindowsPowerShell\Modules\PSWinDocumentation.O365HealthService\0.2.2\PSWinDocumentation.O365HealthService.psm1:307 char:18

[!\[PowerShell error output from Invoke-RestMethod showing the underlying connection was closed during Office 365 API calls.\](/wp-content/uploads/2019/12/img_5df4a87b40896.png)](/wp-content/uploads/2019/12/img_5df4a87b40896.png)[!\[Highlighted PowerShell line in the Office 365 health module where Invoke-RestMethod fails before enforcing TLS 1.2.\](/wp-content/uploads/2019/12/img_5df4a8506b008.png)](/wp-content/uploads/2019/12/img_5df4a8506b008.png)

Since this PowerShell module is all about **Invoke-RestMethod** it means all calls to it are failing.

### 💡 Invoke-RestMethod – TLS 1.0 to TLS 1.2

It seems that the error is coming from** Office 365** change were **TLS 1.0** is being superseded by **TLS 1.2**. Since it's still working for my default configuration, I guess my tenant is not yet forced on **TLS 1.2**, but others already switched. The fix is simple. Before executing **Invoke-RestMethod**, just run this:

```powershell
​[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
```

This change stays active for the whole **PowerShell** session. If you close and reopen **PowerShell**, it will be gone. If you use **Invoke-RestMethod** in your scripts, add it in a function next to the first **Invoke-RestMethod** you use. If you want this always to be available – add it to your **PowerShell** profile. That way, you won't have to worry about it. Just remember to tell others if you send the scripts over. Keep in mind, however, that this change may impact other commands that you use. If you work with some legacy systems that don't support **TLS 1.2**, you may need to work it out before going full-on **TLS 1.2**. If you want to read more about the change, you can read about it [Preparing for TLS 1.2 in Office 365 and Office 365 GCC](https://docs.microsoft.com/en-us/office365/troubleshoot/security/prepare-tls-1.2-in-office-365). While in theory they will start turning off **TLS 1.0** and **TLS 1.1** as of **June 2020**, it seems some tenants and some services got the change a bit earlier.

## Related Projects

Projects that align with this post's tags, categories, and content.

[All projects](/projects/)

Matching projects...

[Back to Blog](/blog)

## Related posts

- [PowerShellGallery disables support for TLS 1.0 breaking Install-Module](/blog/powershellgallery-disables-support-for-tls-1-0-breaking-install-module/)
- [Syncing Global Address List (GAL) to personal contacts and between Office 365 tenants with PowerShell](/blog/syncing-global-address-list-gal-to-personal-contacts-and-between-office-365-tenants-with-powershell/)
- [Easy way to send emails using Microsoft Graph API (Office 365) with PowerShell](/blog/easy-way-to-send-emails-using-microsoft-graph-api-office-365-with-powershell/)

<!-- PowerForge markdown artifact sha256:6432517E8027CE58D54116B9A408175D44C436DE2B69BE26CEE4AA379541EF60 -->
