Skip to content

Microsoft.Graph and PnP.PowerShell conflict (Could not load type 'Microsoft.Graph.Authentication.AzureIdentityAccessTokenProvider') #2285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
duschu opened this issue Sep 4, 2023 · 9 comments

Comments

@duschu
Copy link

duschu commented Sep 4, 2023

Describe the bug
When using the Microsoft.Graph and PnP.PowerShell module in the same script, the Get-MgGroup -All command fails with the following exception:
Get-MgGroup_List: Could not load type 'Microsoft.Graph.Authentication.AzureIdentityAccessTokenProvider' from assembly 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

The error occurs if the script is executed locally as well as an Azure Automation runbook.

As stated in the details below, we locally use PowerShell v7.3.6 and in Azure Automation we also use the latest PS7.2. runbooks.
The latest Microsoft.Graph.* module version 2.4.0 is used as well as a recent PnP.PowerShell 2.2.59-nightly.

To Reproduce
Minimal code example to reproduce the issue:

Import-Module Microsoft.Graph.Authentication
Import-Module Microsoft.Graph.Groups
Import-Module PnP.PowerShell

# Connect to Microsoft Graph and SharePoint via client id + certificate
Connect-MgGraph -TenantId $tenantId -ClientId $clientId -CertificateThumbprint $certificateThumbprint
Connect-PnPOnline -Url $url -Tenant $tenant -ClientId $clientId -Thumbprint $certificateThumbprint

# Get groups (this is the command that will fail)
$groups = Get-MgGroup -All

Expected behavior
Get-MgGroup should return the group objects of the tenants.

Debug Output

DEBUG: [CmdletBeginProcessing]: - Get-MgGroup begin processing with parameterSet 'List'.
DEBUG: [Authentication]: - AuthType: 'AppOnly', TokenCredentialType: 'ClientCertificate', ContextScope: 'Process', AppName: 'dsc-group-lifecycle'.
DEBUG: [Authentication]: - Scopes: [AccessReview.ReadWrite.All, Group.ReadWrite.All, User.Read.All, Mail.Send].
DEBUG: [CmdletException]: Received exception with message 'TypeLoadException - Could not load type 'Microsoft.Graph.Authentication.AzureIdentityAccessTokenProvider' from assembly 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. :    at Microsoft.Graph.PowerShell.Authentication.Helpers.HttpHelpers.GetGraphHttpClient()
   at Microsoft.Graph.PowerShell.Module.BeforeCreatePipeline(InvocationInfo invocationInfo, HttpPipeline& pipeline)
   at Microsoft.Graph.PowerShell.Module.CreatePipeline(InvocationInfo invocationInfo, String parameterSetName)
   at Microsoft.Graph.PowerShell.Cmdlets.GetMgGroup_List.ProcessRecordAsync()'
Get-MgGroup_List: Could not load type 'Microsoft.Graph.Authentication.AzureIdentityAccessTokenProvider' from assembly 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
DEBUG: [CmdletEndProcessing]: - Get-MgGroup end processing.

Module Versions
The following module versions are installed exactly like that both locally under Windows and in the Azure automation account:

Version Name
2.4.0 Microsoft.Graph.Authentication
2.4.0 Microsoft.Graph.Groups
2.4.0 Microsoft.Graph.User
2.2.59-nightly PnP.PowerShell

The nighly version of PnP.PowerShell has been installed by intention since there were known issues regarding module conflicts with Az.* and others in the last weeks, which should have been fixed in latest nightly versions. See links under "additional context" for further information.

Environment Data
Local Windows environment: PSVersion 7.3.6
Azure automation account and runbook with version PS 7.2

Additional context
The problem seems to be related to the following bug reports, but it did not help to solve my issue:

@fpsacha
Copy link

fpsacha commented Sep 5, 2023

I have the same issue and it happens often.

@peombwa
Copy link
Member

peombwa commented Sep 6, 2023

The issue here is with the version of Microsoft.Graph.Core that's loaded by the PnP.PowerShell module. The module loads an outdated version of Microsoft.Graph.Core, Version=1.25.1.0 dll - https://www.nuget.org/packages/Microsoft.Graph.Core/1.25.0. The latest version of Microsoft.Graph.Core is 3.x, which is what Microsoft.Graph PowerShell SDK uses.

➜ [System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object Location | Sort-Object -Property FullName | Select-Object -Property FullName, Location | where FullName -Like "Microsoft.Graph.Core*"

FullName                                                                                 Location
--------                                                                                 --------
Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 C:\Program Files\WindowsPowerShell\Modules\PnP.PowerShell\2.2.60\Core\Microsoft.Graph.Core.dll

Please transfer the issue to the PnP.PowerShell repo as they'll need to update the dll in their module.

As a workaround, you can update your script to ensure that Microsoft.Graph.Core v3.x is always loaded by using Get-MgGroup before importing PnP.PowerShell module.

Import-Module Microsoft.Graph.Authentication
Import-Module Microsoft.Graph.Groups

...
$groups = Get-MgGroup -All # Before PnP.Powershell import.
...

Import-Module PnP.PowerShell

@duschu
Copy link
Author

duschu commented Sep 6, 2023

Thank you, @peombwa, I'm waiting for the response regarding the issue pnp/powershell#3395 in the PnP.PowerShell repo.

@microsoft-github-policy-service
Copy link
Contributor

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

@AndersRask
Copy link

The issue here is with the version of Microsoft.Graph.Core that's loaded by the PnP.PowerShell module. The module loads an outdated version of Microsoft.Graph.Core, Version=1.25.1.0 dll - https://www.nuget.org/packages/Microsoft.Graph.Core/1.25.0. The latest version of Microsoft.Graph.Core is 3.x, which is what Microsoft.Graph PowerShell SDK uses.

➜ [System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object Location | Sort-Object -Property FullName | Select-Object -Property FullName, Location | where FullName -Like "Microsoft.Graph.Core*"

FullName                                                                                 Location
--------                                                                                 --------
Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 C:\Program Files\WindowsPowerShell\Modules\PnP.PowerShell\2.2.60\Core\Microsoft.Graph.Core.dll

Please transfer the issue to the PnP.PowerShell repo as they'll need to update the dll in their module.

As a workaround, you can update your script to ensure that Microsoft.Graph.Core v3.x is always loaded by using Get-MgGroup before importing PnP.PowerShell module.

Import-Module Microsoft.Graph.Authentication
Import-Module Microsoft.Graph.Groups

...
$groups = Get-MgGroup -All # Before PnP.Powershell import.
...

Import-Module PnP.PowerShell

The workaround does work, but it's not really an issue that is unique for PnP.PowerShell module, as I get the exact same issue when using Az!

If you connect to both Az and MgGraph and call Get-AzADUser before calling Get-MgUser you also receive the exact same error.

I actually posted about this in pnp.powershell a year ago, and they did a great job in implementing my suggestion of using the ALC bridging pattern, which got rid of most of the colliding assembly issues. If you are not familiar with it, ALC is the new way of loading assemblies in PowerShell 7, and have a better way of handling multiple versions of the same assembly, and as I understand it, is the "preferred" way of loading assemblies in PowerShell 7 modules in general. So if all module developers use this approach, we wouldn't see most of these conflicts :-)

[https://learn.microsoft.com/en-us/powershell/scripting/dev-cross-plat/resolving-dependency-conflicts?view=powershell-7.3#loading-through-net-core-assembly-load-contexts](Loading through .NET Core Assembly Load Contexts)

pnp/powershell#3207

@nicole-ge
Copy link

Hello,
I'm using in Azure Automation the module 2.4.0 | PnP.PowerShell and it is still loading Microsoft.Graph.Core, Version=1.25.1.0
so the problem is still there. what can I do?

@joostvdlinden
Copy link

I experience the same issue. Was a solution ever found?

@oehi86
Copy link

oehi86 commented Mar 25, 2025

@joostvdlinden
i still have the same issue... :-(

@joostvdlinden
Copy link

joostvdlinden commented Mar 25, 2025

@joostvdlinden i still have the same issue... :-(

I was able to determine that there is an issue with the latest version of Microsoft.Graph PowerShell (v 2.26.1). Link: https://office365itpros.com/2025/03/04/powershell-sdk-problems/
Installing module v 2.25.0 was part of the solution for me.

Additionally, I had to:

  1. run Connect-MgGraph -Identity first
  2. then perform a random action towards Microsoft Graph, i.e. Get-MgUser -UserId %some kind of admin user which will exist permanently%
  3. and at last run Connect-PnPOnline -Url $siteURL -ManagedIdentity

In that specific order within my Azure Automation Runbook. Make sure that Microsoft Graph performs a random action before it is connecting with PnP (source link: #2622). At least it works now for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants