Invoke-MgGraphRequest mit Fehler „Microsoft.Graph.Core, Version=1.25.1.0“

Ich nutze regelmässig die PowerShell Module PnP.PowerShell und Microsoft.Graph.Authentication in Kombination in einem Script. Abhängig wann das Modul für PnP.PowerShell geladen wird zeigt PowerShell 7 bei Invoke-MgGraphRequest folgenden Fehler. Allgemein kann es bei weiteren Modulen von Microsoft Graph auftreten.

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

Eine Google Suche findet in GitHub schnell eine gute Beschreibung über die Ursache.

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.

Es ist erkennbar PnP.PowerShell ladet die veraltete DLL von Microsoft.Graph.Core.

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

Für meine Aufgabe kann ich im Script das PnP.PowerShell Modul nicht erst an einer anderen Stelle laden.
Der Ersteller des Issues hat im PnP Repository ein neues Issues eröffnet. Bedauerlicherweise wurde das Issue von einem PnP Team Member mit einer falschen Begründung geschlossen. Ein weiteres Issue 3735 wurde erstellt.

GitHub

Das Modul von Microsoft.Graph.Authentication ist nicht die Ursache.
Ich probierte es bis zur letzten Nightly Version (2.3.49-nightly) von PnP.PowerShell. Die veraltete DLL im PnP.PowerShell Modul wurde bis heute nicht ersetzt.

Ein Workaround ist die veraltete Datei Microsoft.Graph.Core.dll aus dem Modulverzeichnis von PnP.PowerShell zu löschen. Es ist ein Workaround. Die Datei wird beim nächsten Update des Moduls wieder erstellt.

  1. Schliesse alle geöffneten PowerShell Instanzen (Visual Studio Code inklusive). Die DLL darf nicht geladen sein.
  2. Du kannst die Datei nun entweder manuell über Windows Explorer löschen oder über eine neu geöffnete PowerShell Instanz.
PowerShell
$PnPModuleDirectory = (Get-Module PnP.PowerShell -ListAvailable)[0].ModuleBase
Remove-Item -Path ($PnPModuleDirectory + "\Core\Microsoft.Graph.Core.dll") -Force

Wird die Abfrage mit Invoke-MgGraphRequest danach erneut ausgeführt ladet (statt PnP.PowerShell) das Microsoft.Graph.Authentication Modul die aktuelle DLL. Die Abfrage funktioniert ohne Fehler.

Aktuelle DLL über Microsoft.Graph.Authentication
Aktuelle DLL über Microsoft.Graph.Authentication
Share this post:

About the Author

Tobias Asböck

Tobias Asböck

Tobias is a Senior System Engineer with more than 10 years of professional experience with Microsoft 365 products such as SharePoint Online, OneDrive for Business, Teams Collaboration, Entra ID, Information Protection, Universal Print, and Microsoft 365 Licensing. He also has 15+ years of experience planning, administering, and operating SharePoint Server environments. In his spare time, Tobias is busy with updates in the Microsoft 365 world or on the road with his road bike and other sports activities. If you have additional questions, please contact me via LinkedIn or support@topedia.com.

No comments yet