pnp / powershell

PnP PowerShell
https://pnp.github.io/powershell
MIT License
687 stars 351 forks source link

[BUG] Assembly Conflicts with Microsoft.Graph Module #3637

Open cprovpo opened 11 months ago

cprovpo commented 11 months ago

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: image

Output of assemblies loaded in the session. The two assemblies called out in the MgGraph exception are sourced from the PnP.PowerShell Module image

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?

JNichNP commented 11 months ago

I fixed this by grabbing the Microsoft.Graph.Core.dll from Microsoft.Graph.Authentication and dropping it right into C:\Program Files\PowerShell\Modules\PnP.PowerShell\2.3.8\Core

Not the best, nor the most elegant solution, but it worked.

Jaffa666 commented 11 months ago

I fixed this by grabbing the Microsoft.Graph.Core.dll from Microsoft.Graph.Authentication and dropping it right into C:\Program Files\PowerShell\Modules\PnP.PowerShell\2.3.8\Core

Not the best, nor the most elegant solution, but it worked.

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.

cprovpo commented 11 months ago

Thanks for the suggested workarounds - I'll be sure to test those. I am concerned about the lengthy regression testing that might be required by replacing these, as I'm not sure if the replaced DLLs will cause issues with the rest of my code.

cprovpo commented 10 months ago

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.

cprovpo commented 10 months ago

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

$users = Invoke-PnPGraphMethod -Url "/users"
$applications = Invoke-PnPGraphMethod -Url "/applications"

Hopefully this helps someone else!

fasteiner commented 10 months ago

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.

Error Message when connecting to graph, after connecting to PNP: ERROR - Connect to Microsoft Graph failed.(TenantId: tttech365.onmicrosoft.com, AppId ****) ERROR - Connect-MgGraph: Line | 135 | Connect-MgGraph -TenantId $AzureTenant -ClientId $AppId - … | ~~~~~~~~~~~~~ | ClientCertificateCredential authentication failed: Could not load type 'Microsoft.Identity.Client.BaseAbstractApplicationBuilder`1' from assembly 'Microsoft.Identity.Client, Version=4.50.0.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae'.

gkDoSt commented 9 months ago

Any updates on this? Im facing the same issue :/

MichaMican commented 9 months ago

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

HeKr09 commented 9 months ago

Is there anything new? This is a really annoying bug.

rvanandel-hcg commented 9 months ago

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.

crnnr commented 9 months ago

Is there a timeline for this? Also facing this issue with workaround not feasible :///

jdarre commented 9 months ago

Any update here? As of now, we have to run a single server with PnP.Powershell 2.3.0 and version 1.28 of the Mg-Module.

Legitage commented 9 months ago

Our automation scripts are also failing with the same error and for the same reason. Another short-term (not ideal) workaround for using PnP 2.3 is to uninstall the MS Graph module and/or install Microsoft.Graph 2.8.

This DLL version conflict needs to be resolved in way that it doesn't keep happening every few updates.

cyrilchotard commented 8 months ago

Same issue for me in Azure Automation with PnP.PowerShell 2.4.0 and Microsoft.Graph 2.15.0

kpinel commented 8 months ago

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.

PowerShell 7.4.1
PS > connect-mgGraph -TenantId GUID -NoWelcome
PS > get-mguser -UserId user@domain.com

DisplayName Id          Mail               UserPrincipalName
----------- --          ----               -----------------
DigitalUser User-GUID   user@domain.com    user@domain.com

PS > Import-Module PnP.PowerShell
PS > get-mguser -UserId user@domain.com

DisplayName Id          Mail               UserPrincipalName
----------- --          ----               -----------------
DigitalUser User-GUID   user@domain.com    user@domain.com

PS > connect-pnpOnline -Url tenant.sharepoint.com -Interactive
PS > get-mguser -UserId user@domain.com

DisplayName Id                                   Mail               UserPrincipalName
----------- --                                   ----               -----------------
DigitalUser User-GUID                            user@domain.com    user@domain.com

PS > Get-PnPTenantSite -Url https://tenant.sharepoint.com/teams/msteams_50e441

Url                                                       Template LocaleId
---                                                       -------- --------
https://tenant.sharepoint.com/teams/msteams_50e441        GROUP#0  1033

set-pnptenantSite -Url https://tenant.sharepoint.com/teams/ACA_TM_SSTALeadershipGroup-LwpsJ -LockState unlock
WARNING: You changed the lockstate of a site. This change is not guaranteed to be effective immediately. Please wait a few minutes for this to take effect.
PS > set-pnptenantSite -Url https://tenant.sharepoint.com/teams/ACA_TM_SSTALeadershipGroup-LwpsJ -LockState NoAccess
WARNING: You changed the lockstate of a site. This change is not guaranteed to be effective immediately. Please wait a few minutes for this to take effect.
jdenka commented 8 months ago

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.

kpinel commented 8 months ago

@Dennis, My workaround is still working. I currently have a session open to SharePoint, Graph, and EXO and haven't had any issue. The main thing is that the Graph commands are executed first. Pnp then uses that token provider in preference to the one bundled with it.

Kind regards, Kevin

If you're going to be passionate about something, be passionate about learning

On Thu, 14 Mar 2024 at 19:46, Dennis Johansson @.***> wrote:

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.

— Reply to this email directly, view it on GitHub https://github.com/pnp/powershell/issues/3637#issuecomment-1997043125, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAGVVB6ZWTHV2UXQRTJ6FSLYYFWW5AVCNFSM6AAAAABAW5IDHOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSOJXGA2DGMJSGU . You are receiving this because you commented.Message ID: @.***>

dennis commented 8 months ago

Keep in mind, that writing @ notifies that user of your message :)

jdenka commented 8 months ago

@dennis, My workaround is still working. I currently have a session open to SharePoint, Graph, and EXO and haven't had any issue. The main thing is that the Graph commands are executed first. Pnp then uses that token provider in preference to the one bundled with it. Kind regards, Kevin ------------------------------- If you're going to be passionate about something, be passionate about learning On Thu, 14 Mar 2024 at 19:46, Dennis Johansson @.> wrote: 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. — Reply to this email directly, view it on GitHub <#3637 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAGVVB6ZWTHV2UXQRTJ6FSLYYFWW5AVCNFSM6AAAAABAW5IDHOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSOJXGA2DGMJSGU . You are receiving this because you commented.Message ID: @.>

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.

cornettew commented 7 months ago

+1

jgriff500 commented 7 months ago

This is so stinking frustrating.....

Someone please explain why "Connect-MgGraph" loads Pnp.Powershell assemblies?

jgriff500 commented 7 months ago

@kpinel -- Are you using Visual Studio Code or running scripts from pwsh.exe?

If I have the Pnp.PowerShell module installed, I can't even Connect-MgGraph - I get the same error as the OP. If I uninstall Pnp.PowerShell my Connect-MgGraph works fine.

kpinel commented 7 months ago

It doesn't matter. It's PowerShell regardless.

Also, graph doesn't load PNP assemblies. They both use the same assemblies. Graph happens to use the newer ones so if you connect to graph first, the PNP modules won't load the older assemblies included with it.

On Sat, 13 Apr 2024, 06:06 jgriff500, @.***> wrote:

@kpinel https://github.com/kpinel -- Are you using Visual Studio Code or running scripts from pwsh.exe?

If I have the Pnp.PowerShell module installed, I can't even Connect-MgGraph - I get the same error as the OP. If I uninstall Pnp.PowerShell my Connect-MgGraph works fine.

— Reply to this email directly, view it on GitHub https://github.com/pnp/powershell/issues/3637#issuecomment-2052452727, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAGVVB3SR6GX6D4VNF2YUCTY5A5EHAVCNFSM6AAAAABAW5IDHOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANJSGQ2TENZSG4 . You are receiving this because you were mentioned.Message ID: @.***>

jgriff500 commented 7 months ago

@kpinel my experience sounds slightly different.

From Visual Studio Code, Connect-MgGraph fails if PnP.PowerShell is installed (without importing or issuing any commands). I can do a fresh PS session, Connect-MgGraph and see there are Pnp.PowerShell assemblies loaded.

If I uninstall Pnp.PowerShell, Connect-MgGraph works perfectly.

I have a script that uses MgGraph and PnP.Powershell -- if I run from PS 7 (right click, open with PWSH), my experience mimics others -- run Connect-MgGraph first, run a Mg-Get command, THEN import PnP.Powershell.

TobiasAT commented 7 months ago

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.

SDooof commented 7 months ago

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.

Thank you, this solved my pain and agony in trying to figure out a way to make both pnp and graph modules work in 1 script!

jgriff500 commented 7 months ago

Thank you @TobiasAT. This is a tremendous help.

tschlienger commented 7 months ago

Just bumped into this issue with a colleague and was immediately able to solve it using the workaround script from @TobiasAT. Thank you very much!

nicole-ge commented 7 months ago

Hi @gautamdsheth or @veronicageek

This Bug is still open and not working causa the pnp module is loading the wrong ms graph DLL. My azure automation failed .

Please help a lot of Bugs to this issues are reporter

Thank you in advance

RobIreland68 commented 7 months ago

Seeing the same issue, my DEV VM is Windows 11 and has the following installed

PowerShell Version: 7.4.2 PnP PowerShell Version: 2.4.0 Microsoft.Graph Version: 2.18.0 Microsoft.Graph.Beta Version: 2.18.0 Microsoft.Identity.Client: 4.53.0

The error happens in the following line of code

$eid = get-mggroup -consistencylevel eventual -search $groupSearchTerm …

     | Could not load type 'Microsoft.Graph.Authentication.AzureIdentityAccessTokenProvider' from assembly      
     | 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

Consistently happens when I'm running the script via VS Code Terminal Window (Powershell 7.4.2 process) or from my Terminal Window (PowerShell 7.4.2).

I have tried TobiasAT's script (Above)

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.

I get the following errors

     | Unable to load one or more of the requested types. Could not load file or assembly 'Microsoft.Graph.Core,
     | Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.

     | Could not load type 'Microsoft.Graph.ICollectionPage`1' from assembly 'Microsoft.Graph.Core, Version=3.0.9.0,
     | Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Could not load type 'Microsoft.Graph.ICollectionPage`1' from
     | assembly 'Microsoft.Graph.Core, Version=3.0.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Could not
     | load type 'Microsoft.Graph.ICollectionPage`1' from assembly 'Microsoft.Graph.Core, Version=3.0.9.0,
     | Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Could not load type 'Microsoft.Graph.ICollectionPage`1' from
     | assembly 'Microsoft.Graph.Core, Version=3.0.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Could not
     | load type 'Microsoft.Graph.ICollectionPage`1' from assembly 'Microsoft.Graph.Core, Version=3.0.9.0,
     | Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
saintoctopus commented 6 months ago

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.

Amazing! Thank you so much!! ❤️

Rwarcards762 commented 6 months ago

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.

@TobiasAT -- huge props for this. Great workaround. Thank you!!

Roladin commented 6 months ago

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

HeKr09 commented 6 months ago

@TobiasAT Although it was a big help, I encountered an issue after running the script.

Get-PnPSiteTemplate: D:\TFS\SWO.PortalServices\Customer Creation\Export-SwoPortalTemplate.ps1:161
Line |
 161 |      Get-PnPSiteTemplate -Out $fullPath -Schema LATEST -Encoding ([Sys …
     |      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Unable to load one or more of the requested types. Could not load file or assembly 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral,
     | PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. Could not load file or assembly 'Microsoft.Graph.Core, Version=1.25.1.0,
     | Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. Could not load file or assembly 'Microsoft.Graph.Core,
     | Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. Could not load file or assembly
     | 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. Could not load file
     | or assembly 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. Could
     | not load file or assembly 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file
     | specified. Could not load file or assembly 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot
     | find the file specified. Could not load file or assembly 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The
     | system cannot find the file specified. Could not load file or assembly 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral,
     | PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. Could not load file or assembly 'Microsoft.Graph.Core, Version=1.25.1.0,
     | Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. Could not load file or assembly 'Microsoft.Graph.Core,
     | Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. Could not load file or assembly
     | 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. Could not load file
     | or assembly 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. Could
     | not load file or assembly 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file
     | specified. Could not load file or assembly 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot
     | find the file specified. Could not load file or assembly 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The
     | system cannot find the file specified. Could not load file or assembly 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral,
     | PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. Could not load file or assembly 'Microsoft.Graph.Core, Version=1.25.1.0,
     | Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. Could not load file or assembly 'Microsoft.Graph.Core,
     | Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. Could not load file or assembly
     | 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. Could not load file
     | or assembly 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. Could
     | not load file or assembly 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file
     | specified.
Write-Error: XML does not exist. Error during site template export.

I really don't know what the problem is to update the dll within PnP...

nicole-ge commented 6 months ago

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

@Roladin You can call the MS Graph via RestAPI. So you dont need to load the module

HeKr09 commented 6 months ago

@TobiasAT Although it was a big help, I encountered an issue after running the script.

Get-PnPSiteTemplate: D:\TFS\SWO.PortalServices\Customer Creation\Export-SwoPortalTemplate.ps1:161
Line |
 161 |      Get-PnPSiteTemplate -Out $fullPath -Schema LATEST -Encoding ([Sys …
     |      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Unable to load one or more of the requested types. Could not load file or assembly 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral,
     | PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. Could not load file or assembly 'Microsoft.Graph.Core, Version=1.25.1.0,
     | Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. Could not load file or assembly 'Microsoft.Graph.Core,
     | Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. Could not load file or assembly
     | 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. Could not load file
     | or assembly 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. Could
     | not load file or assembly 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file
     | specified. Could not load file or assembly 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot
     | find the file specified. Could not load file or assembly 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The
     | system cannot find the file specified. Could not load file or assembly 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral,
     | PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. Could not load file or assembly 'Microsoft.Graph.Core, Version=1.25.1.0,
     | Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. Could not load file or assembly 'Microsoft.Graph.Core,
     | Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. Could not load file or assembly
     | 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. Could not load file
     | or assembly 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. Could
     | not load file or assembly 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file
     | specified. Could not load file or assembly 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot
     | find the file specified. Could not load file or assembly 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The
     | system cannot find the file specified. Could not load file or assembly 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral,
     | PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. Could not load file or assembly 'Microsoft.Graph.Core, Version=1.25.1.0,
     | Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. Could not load file or assembly 'Microsoft.Graph.Core,
     | Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. Could not load file or assembly
     | 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. Could not load file
     | or assembly 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. Could
     | not load file or assembly 'Microsoft.Graph.Core, Version=1.25.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file
     | specified.
Write-Error: XML does not exist. Error during site template export.

I really don't know what the problem is to update the dll within PnP...

The same with Invoke-PnPSiteTemplate

gkDoSt commented 6 months ago

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.

Roladin commented 6 months ago

@nicole-ge thanks, good point. MS Graph REST API with PnP is a decent option at the moment for Azure Automation. If my weird system-generated runtime environment breaks I might have to go down that path :)

Roladin commented 5 months ago

Anyone from the PnP team available to comment on this issue?

BlackC0ffee commented 5 months ago

I can confirm this bug. :(

rmangini commented 5 months ago

Just to reiterate, this is EXTREMELY ANNOYING. Please PNP team.

MichaMican commented 5 months ago

I guess they could port it to use Assembly Load Context (ALC) but that would require a TON of porting work. I am not even sure if there is a proper different way - because without ALC you will always conflict with some Versions of other Modules that target a different Dependency Version...

And as this is a non official/Open Source module i don't even know if it's realistic to hope that this will be implmented someday :/

devops-fan commented 5 months ago

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.

Thank you so much Tobias! I had the same problem using MS Graph module and PnP in same script, but now fixed with your resolution!

TobiasAT commented 5 months ago

PnP.PowerShell module has been updated to version 2.5 last Thursday.

I checked known conflicts with the updated module.

TobiasAT commented 5 months ago

@HeKr09 For your case, or if the command requires the Microsoft.Graph.Core.dll from PnP.PowerShell module, you may have to split your script into two separate processes. One process uses just the Microsoft.Graph.Core.dll from PnP.PowerShell module, the other the updated Microsoft.Graph.Core.dll version.

If you mix the different versions of Microsoft.Graph.Core.dll in one process, the script will have conflicts again.

vnerdahl commented 4 months ago

I had a conflict between the powershell Graph and ExchangeOnline modules. I downgraded the ExchangeOnline to version 3.4.0 and it resolved the conflict.

kpinel commented 4 months ago

@vnerdahl What version of Graph are you using? I'm not having any issues using EXO 3.5.0 and Graph 2.19.0 together

vnerdahl commented 4 months ago

@vnerdahl What version of Graph are you using? I'm not having any issues using EXO 3.5.0 and Graph 2.19.0 together I was having a conflict with Microsoft.Graph 2.19.0 and ExchangeOnlineManagement 3.5.0.

akbazuka commented 4 months ago

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:

$pnpJob = Start-Job -ScriptBlock {
    Import-Module PnP.PowerShell

    #Get all sites using PnP
    $tenantUrl = "https://tenant-admin.sharepoint.com"
    Connect-PnPOnline -Url $tenantUrl 

    $allSites = Get-PnPTenantSite
    return $allSites
}

#Run PnP operations in a separate job
$allSites = Receive-Job -Job $pnpJob -Wait
Remove-Job -Job $pnpJob

#Connect to MS Graph
Connect-MgGraph

#Rest of graph logic... E.g.

$allSites | ForEach-Object {
    Get-MgSite -Search $_.Url
}
matthiashaldimann commented 3 months ago

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?