powershellmicrosoft-graph-apipnp.powershell

Connect-MgGraph cmdlet throws different errors in PowerShell 5.1 and 7.4


I have a PowerShell script that connects to Microsoft.Graph through certificate based authentication

Connect-MgGraph -ClientId "XXXXXX" -TenantId "XXXXX" -CertificateThumbprint "XXXXX"

This is the first error that I encounter when using PowerShell version 5.1: Connect-MgGraph : The type initializer for 'Azure.Core.Pipeline.DiagnosticScopeFactory' threw an exception.

This is the second error that I encounter when using PowerShell version 7.4.1 'Could not load file or assembly 'Microsoft.Identity.Client, Version=4.50.0.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae'

Other Details:

  1. I also run the Connect-MgGraph alongside with the Connect-PnpOnline and Connect-ExchangeOnline in the same script
  2. Microsoft.graph version is the latest -2.14.1
  3. Pnp.PowerShell version is 1.12.0 in PowerShell 5.1 and 2.3.62 in PowerShell 7.4

Solution

  • I had this same issue and the fix for me was to run Disconnect-PnPOnline before trying to connect with Graph. Since they are using the same app registration, it must not allow you doing both types of connections.