powershell connect to o365

Tiago Oliveira 1 Reputation point
2022-02-15T13:10:56.307+00:00

HI!
When i run the following comand to connect o office 365 by powershell i get the error bellow.
It's been a while since i use ps to connect to o365 can anyone help me with what is missing?

Import-Module MSOnline
$TenantUname="user"
$TenantPass = ConvertTo-SecureString "pass" -AsPlainText -Force
$TenantCredentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $TenantUname, $TenantPass
Connect-MsolService -Credential $TenantCredentials

Error:
Connect-MsolService: Could not load type 'System.Security.Cryptography.SHA256Cng' from assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2022-02-15T16:02:34.92+00:00

    Are you using PowerShell 7? That object type isn't supported by .Net Core.


  2. Limitless Technology 39,916 Reputation points
    2022-02-16T15:41:14.777+00:00

    Hello @Tiago Oliveira

    I would recommend to check which version of Powershell you are using by running $PSVersionTable in Powershell.

    If you are running version 7, you may need to install and update the AzureAD module to work succesfully. Run the next from elevated Powershell window:

    Install-Module AzureAD
    Import-Module AzureAD -UseWindowsPowerShell

    Hope this helps with your query,

    --
    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments

  3. Tiago Oliveira 1 Reputation point
    2022-02-17T12:54:32.777+00:00

    Hi!

    i fixed the script by changing it to the following

    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    Import-Module MSOnline -UseWindowsPowerShell
    $TenantUname="user"
    $TenantPass = ConvertTo-SecureString "pass" -AsPlainText -Force
    $TenantCredentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $TenantUname, $TenantPass
    Connect-MsolService -Credential $TenantCredentials

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.