After upgrading Microsoft Graph, I noticed an issue when trying to run cmdlet Get-MgGroup or Get-MgUser. I could connect to Graph with no issues, but multiple cmdlets failed.
Error
Get-MgGroup : Could not load type 'Microsoft.Graph.Authentication.AzureIdentityAccessTokenProvider' from assembly 'Microsoft.Graph.Core, Version=1.25
.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
At line:1 char:1
+ Get-MgGroup -All
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-MgGroup_List], TypeLoadException
+ FullyQualifiedErrorId : Microsoft.Graph.PowerShell.Cmdlets.GetMgGroup_List
This command did also fail
[Microsoft.Graph.Authentication.AzureIdentityAccessTokenProvider]
CategoryInfo : InvalidOperation: (Microsoft.Graph…ssTokenProvider:TypeName) [], RuntimeException
FullyQualifiedErrorId : TypeNotFound
Root Cause
Even though Microsoft Graph v2.2 installed the version 3.0.9.0 of Microsoft.Graph.Core.Dll, there were multiple versions of Microsoft.Graph.Core.dll found in the $Env:PsModulePath path.
A search found multiple older versions (1.11, 1.24, 1.25) of Microsoft.Graph.Core.dll related to PnP.Powershell module.
Resolution
Remove older versions of Pnp.Powershell (or other Powershell-modules having old DLLs)
uninstall-module -Name PnP.PowerShell -AllVersions
Issue: Uninstall – Module x is currently in use ….
If you see the below error, it is caused by a dependent DLL being loaded as part of the shell / session and therefore in use.
Solution: Module x is currently in use ….
Method 1: Remove-Module + Uninstall-Module
Run the Remove-Module first to unload the imported module from memory.
After this you can do an uninstall-module
Remove-Module -Name <ModuleName> -force
Uninstall-Module -Name <ModuleName> -force
Method 2: Start a CMD and then a Powershell with -NoProfile
(1) Close all running Powershell sessions
(2) Start a CMD as local admin
(3) Run the below Powershell-command with the -NoProfile and -NonInteractive. Powershell will now be started, without loading any DLLs into the session.
C:\>powershell -NoProfile -NonInteractive -Command "uninstall-module -Name PnP.PowerShell -AllVersions"
This was exactly my problem. Thanks!
Thank you Dude! it solves my issue
Hello
In Azure Automation using pnp 2.4.0 module this bug still appear in combination with graph. What can I do?
it appears to be a problem in v2.18 – try to downgrade your graph to v2.17
It looks like MSTF is aware of an issue.
https://pnp.github.io/powershell/articles/azureautomationrunbook.html#specific-stable-version
“Currently stable PnP PowerShell version that works with Azure Automation 7.2 Runbooks is 2.1.1. If you would like to use a latest nightly build instead, use the below instructions”
Removing PnP.PowerShell 2.4.0 from the automation account runtime env. resolved the issue.
From my pc:
Install-Module -Name PnP.PowerShell -RequiredVersion 2.1.1
Save-Module -Name PnP.PowerShell -RequiredVersion 2.1.1 -Path c:\example
Zip the exported folder
Upload to the automation account
Thank you!
Was having this issue on my VS Code. I have the VS Code System Installer.
I uninstalled PnP.PowerShell v2.4.0 and installed v2.2.0 instead as it has no issues with that.
It does also have issues with v2.3.0
Uninstall-Module -Name PnP.PowerShell -Force
Install-Module -Name PnP.PowerShell -Force -Scope AllUsers -RequiredVersion 2.2.0
Thanks, downgrading to 2.2.0 solved the issue as well. So many hours lost. Hope others will find this blog post.
Glad it helped 🙏
For others who experience the same issue. I was having the same problem using the latest versions of PNP.PowerShell (2.10) and Graph (2.22) in a runbook (Automation Account). I fixed it by executing dummy cmdlet right after connecting to PNP ($test = Get-MgUser -top 10). So it looked like that: Connect-MgGraph, $test = Get-MgUser -top 10, Connect-PnPOnline.
Somehow it fixed the problem, because I don’t have any outdated modules and only a few graph ones.