Powershell: TLS 1.2 and Office 365 / Azure
If you have or thinking about switching over to TLS 1.2 fully in your environment, you may have encountered or going to encounter some issues with PowerShell and TLS 1.2. A few weeks ago, I had a few PowerShell scripts fail to connect properly to Office 365 and Azure AD. We had just switched over to TLS 1.2 only on our environment.
The issue was trying to pass a variable with “Get-Credential” to “Connect-MsolService” with the -Credential flag, the login would fail. If I connected without the –Credential flag PowerShell would prompt to login which works but when you are connecting to multiple systems using a variable to pass, credentials is very handy.
I posted on twitter to see if anyone else had this issue. I got a reply from Steve Syfuhs (@SteveSyfuhs). He states that certain versions of PowerShell does not support TLS 1.2 out of the box. His solution was to force powershell to use TLS 1.2 and to apply the following code at the top of your script
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
After applying this one line of code to my script, I ran the script and everything worked like it should. I hope this one liner helps others like it helped me. Big thanks to Steve Syfuhs (@SteveSyfuhs).