powershell: [BUG] Assembly Conflicts with Microsoft.Graph Module
Reporting an Issue or Missing Feature
Running Microsoft.Graph cmdlets, such as Connect-MgGraph and Get-MgApplication, produce an assembly conflict error if run after PnP.PowerShell assemblies have already been loaded.
Expected behavior
According to release notes for PnP.PowerShell 2.3.0, assembly conflicts have been resolved. With this being true, I was hopeful I could develop a script that used cmdlets from both PnP.PowerShell and Microsoft.Graph. In my scenario, this doesn’t appear to be the case.
Actual behavior
Results from trying to run Connect-MgGraph and Get-MgApplication after PnP had already been loaded:
Output of assemblies loaded in the session. The two assemblies called out in the MgGraph exception are sourced from the PnP.PowerShell Module
Steps to reproduce behavior
Import-Module PnP.PowerShell
Connect-PnPOnline -Url $url -Interactive
Disconnect-PnPOnline
Import-Module Microsoft.Graph
Connect-MgGraph
Connect-MgGraph -TenantId $TenantId -ClientSecretCredential $ClientSecretCredential
Get-MgApplication
Disconnect-MgGraph
What is the version of the Cmdlet module you are running?
PnP.Powershell - 2.3.3 Nightly Build Microsoft.Graph.Authentication - 2.9.0 Microsoft.Graph.Applications - 2.9.0
Which operating system/environment are you running PnP PowerShell on?
- [x ] Windows
- Linux
- MacOS
- Azure Cloud Shell
- Azure Functions
- Other : please specify
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 7
- Comments: 63 (1 by maintainers)
I had the same issues and (for a workaround) wrote a PowerShell script to fix two known issues between the PnP.PowerShell and Microsoft.Graph.Authentication modules. If interested, read Resolve-TAPnPPowerShellConflicts in my repository.
Hello all, thank you for the patience. Took quite a while , but we finally removed the Graph dependency which was causing the conflict. Would encourage you to update to the latest nightly builds (3.1.36-nightly or later ) and let us know.
Thank you for the good workaround. It is very suitable for some cases, rather difficult to implement in others. Is there a reason why PnP.PowerShell does not come with an up-to-date Microsoft.Graph.Core.dll or - even better - use the “original” Graph module instead of providing it’s own copy of Graph SDK components?
For anyone looking for a workaround to use the PnP module and the Graph module in the same script, one way is to start a separate job within a script that encapsulates the import of one of the modules and run any of that module’s cmdlets within it. For example, in the case where most of your logic lies on GraphAPI side, but you want to first get some sites from SharePoint, you can try the following solution:
Facing same issues here, my workflow is that I get information from Sharepoint with PNP Powershell and then connecting to Graph, this is not working properly for obvious resons.
Seems like there are quite a few people that are facing the same issues here. Have tried to use some workarounds but have not got anything to work except to use really old versions of either Graph (1.25) or go back to PNP Powershell version 1.x.
That should not be the solution to this issue.
Is there anything new? This is a really annoying bug.
The issue persists with the new version of graph (2.12.0) and PNP 2.3.0, could you please fix this issue? otherwise people are forced to work with the old PNP version 1.12.0.
Same issue here. Absolutely horrible bug tbh. This is currently a hard blocker for us as for security and regularoty reasons we may not tamper with module files of modules, hence we can’t work with the workarround by @JNichNP
Add me to the list of people who are experiencing this issue. Brand new server with the lastest versions of PNP and Graph modules. There’s gotta be a way to fix this. Seems both Graph and PNP teams are blaming the other team rather than coming up with a solution.
Yes, you have to connect to Microsoft Graph prior doing anything. This will force the assembly to load and PnP will use it. The opposite will not work. The issue with Azure Automation Runbook and Azure Function is that the powershell runtime is not restarted for each call. You run in the same powershell. So if a script connect to PnP and later another wants to connect to Graph it will fail.
PnP.PowerShell module has been updated to version 2.5 last Thursday.
I checked known conflicts with the updated module.
Unfortunately, I can confirm @HeKr09 issue with the Invoke-PnPSiteTemplate cmdlet… Is there already a timetable for the whole issue if and when the whole problem will be fixed? Unfortunately, this issue has been open since december last year without any results.
This is so stinking frustrating…
Someone please explain why “Connect-MgGraph” loads Pnp.Powershell assemblies?
@gautamdsheth Since Microsoft.Graph.Core.dll is no longer included in the package for 3.1.34-nightly+, unlike 3.1.33-nightly, and removing this file solves at least one of these errors, I’d say it’s looking promising!
From my initial testing I seem to be able to use PnP.PowerShell nightly alongside Graph without deleting files. Thank you!
Now that there is v3 available: Does anyone knows whether the issue persists?
Found this to be an awesome solution as it occurs for me in VsCode and the issue is that the PS extension is loading the modules and resulting in the wrong assemblies being loaded.
Essentially you set VSCode to run the file in a new terminal, rather than use the one the extension lives in.
I changed my launch to contain this. The
createTemporaryIntegratedConsolebeing the key entry.You cannot handle too much the modules if you use the requirements.psd1 file unless you download both module in the modules folder and upload it with your zip file, which is not nice.
What I did so far, is that I always connect to mgraph prior to connect to pnp-online.
Connect-MgGraph -ManagedIdentity -NoWelcome $PnPAdminConnection = Connect-PnPOnline -Url https://xxxxxx-admin.sharepoint.com -ManagedIdentity -ReturnConnection $PnPSiteConnection = Connect-PnPOnline -Url $site -ManagedIdentity -ReturnConnection
This way, the right assembly is loaded. I did that for all my scripts in my Azure functions that are in the webapp.
I agree with @veronicageek , managing the assembly dependency is a nightmare between Az/Graph/PnP
We also have this issue.
In my test environment I can confirm this combination of modules will fail with the error below.
Azure Automation PS 7.2 Runtime Environment modules. This is a custom runtime environment. Az 11.2.0 Azure CLI 2.56.0 Microsoft.Graph.Authentication 2.19.0 Microsoft.Graph.Identity.SignIns 2.19.0 Microsoft.Graph.Users.Actions 2.19.0 PnP.PowerShell 2.4.0
Runbook output: Get-MgIdentityConditionalAccessPolicy_List: Line | 36 | $CAPs = Get-MgIdentityConditionalAccessPolicy | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | Could not load type ‘Microsoft.Graph.Authentication.AzureIdentityAccessTokenProvider’ from assembly ‘Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’.
It doesn’t matter what order modules are loaded in the runbook.
In an older AA environment we have, the system-generated runtime environments are different and luckily the 7.2 runtime has the old 1.25.0 graph module still in it along with pnp 2.x.0 (hopefully I remembered this accurately, system-generated runtimes give the version simply as “Custom” with a date and size. All modules are from a year ago); automations here still work for the moment. Still, they are using old versions which is not ideal long-term.
I don’t want to compromise on security by having runbooks run on-prem on self-hosted runners if they don’t need to touch on-prem resources. If I did this, I would have to use cert-based app reg auth and a non-standard pnp module which may cause other issues down the line. Much more preferable for Azure Automation runbooks is using the latest versions of graph and pnp directly from PSGallery along with the security of using Azure Managed Identities for API access as we currently do when possible.
In Azure Automation runtime environments, it may be a case of uploading a custom version of the pnp module with the offending Microsoft.Graph.Core files removed as per the workaround from @TobiasAT (great writeup, thanks mate). I haven’t tested this and runtime environments are still in preview so implementation could change.
Keen to hear any updates/experiences in AA for this issue.
Thank you
The workflow i’m running does not support that since I have to go to SharePoint first and that breaks everything. This should not be a issue and should be fixed so both of the modules can be used and used in what order needed for the current workflow. It is a issue, that the PNP.PowerShell team needs to fix.
A method that worked for me was to connect to Graph, make a call and then connect to PnP.
This is the quick test that I did and have been using the same session for PnP and Graph calls.
Same issue for me in Azure Automation with PnP.PowerShell 2.4.0 and Microsoft.Graph 2.15.0
One final update - I think I may have found a valuable workaround tool, and it’s built in:
Invoke-PnPGraphMethod
I was just able to pull Users and Applications from Graph API without any assembly conflicts. I did not know this cmdlet exists, but it seems like an excellent workaround. A quick sample of a couple commands I ran
Hopefully this helps someone else!
As an update to my previous post, I have tried the DLL replacements as indicated above. While this resolves the authentication issue, calling other Graph cmdlets (such as Get-MgUser) reveals other assembly conflicts
Get-MgUser_List: Method not found: 'Void Microsoft.Graph.Authentication.AzureIdentityAccessTokenProvider..ctor(Azure.Core.TokenCredential, System.String[], Microsoft.Kiota.Authentication.Azure.ObservabilityOptions, System.String[])'.Unfortunately, we are at a crossroads in getting utility out of both PnP.PowerShell and Microsoft.Graph.
Also grab the Microsoft.Identity.Client.dll from Microsoft.Graph.Authentication and replace the PnP version. This resolved Connect-MGGRAPH error trying to use the older dll files from the PnP module if it was called first.