Pages

Wednesday, February 2, 2022

Connect-AzureAd fails in PowerShell version 7 with: "Could not load type 'System.Security.Cryptography.SHA256Cng'..."

Problem

You attempt to use the cmdlet Connect-AzureAd (after Import-Module AzureAd) to connect to Azure:

Connect-AzureAds

image

… but quickly notice that it fails with the following error:

PS C:\Git\Azure\Azure> Connect-AzureAD

Connect-AzureAD: One or more errors occurred. (Could not load type 'System.Security.Cryptography.SHA256Cng' from assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.): Could not load type 'System.Security.Cryptography.SHA256Cng' from assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

Connect-AzureAD: One or more errors occurred. (Could not load type 'System.Security.Cryptography.SHA256Cng' from assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.)

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

Connect-AzureAD: One or more errors occurred. (Could not load type 'System.Security.Cryptography.SHA256Cng' from assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.): Could not load type 'System.Security.Cryptography.SHA256Cng' from assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

image

This happens in a PowerShell version 7 terminal as well as Visual Studio Code’s PowerShell 7 terminal:

Version: 1.63.2 (user setup)

Commit: 899d46d82c4c95423fb7e10e68eba52050e30ba3

Date: 2021-12-15T09:40:02.816Z

Electron: 13.5.2

Chromium: 91.0.4472.164

Node.js: 14.16.0

V8: 9.1.269.39-electron.0

OS: Windows_NT x64 10.0.19043

image

The PowerShell 7 version is displayed below:

PS C:\Git\Azure\Azure> $PSVersionTable

Name Value

---- -----

PSVersion 7.2.1

PSEdition Core

GitCommitId 7.2.1

OS Microsoft Windows 10.0.19043

Platform Win32NT

PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}

PSRemotingProtocolVersion 2.3

SerializationVersion 1.1.0.1

WSManStackVersion 3.0

image

This is not a problem in PowerShell version 5:

PS C:\> $PSVersionTable

Name Value

---- -----

PSVersion 5.1.19041.1320

PSEdition Desktop

PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}

BuildVersion 10.0.19041.1320

CLRVersion 4.0.30319.42000

WSManStackVersion 3.0

PSRemotingProtocolVersion 2.3

SerializationVersion 1.1.0.1

image

Solution

To get around this issue, use the -UseWindowsPowerShell switch when importing the AzureAD module as such:

PS C:\Git\Azure\Azure> Import-Module AzureAD -UseWindowsPowerShell

WARNING: Module AzureAD is loaded in Windows PowerShell using WinPSCompatSession remoting session; please note that all input and output of commands from this module will be deserialized objects. If you want to load this module into PowerShell please use 'Import-Module -SkipEditionCheck' syntax.

image

Connect-AzureAD should now complete:

image

4 comments:

Anonymous said...

Thanks, worked first time

Evi said...

Thank you Terence - that was a huge help!

9xcrack said...

Great work you done.
PowerShell Studio crack 6/

Anonymous said...

thanks a lot!