If you have gotten the "Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send." error while using PowerShell then add the line below to your script.
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
PowerShell calls uses TLS 1.0 for web requests by default. However, Exchange is expecting a higher level of TLS, so you need to tell PowerShell to use 1.2 instead of the default of TLS 1.0.
Below are links for additional information.
Preparing to use TLS 1.2 in Office 365