entra-powershell
entra-powershell copied to clipboard
Assembly with same name is already loaded
While I've worked with Entra PowerShell, Visual Studio code has crashed. After starting a new session, Entra PowerShell command couldn't run. It failed with the "Assembly with same name is already loaded" error. Entra PowerShell command was the first executed in that new session and Get-Module executed after didn't show any Microsoft.Graph module loaded.
Even after the restart of the VM, I couldn't run Entra PowerShell commands.
Explicitly loading the module with Import-Module Microsoft.Graph.Entra doesn't provide any additional details about the problem.
Even specifying -Prefix fails with the Assembly with same name is already loaded.
I'm also experiencing lot of errors like this
Usually opening a new terminal can work, but not always.
I also tried to remove all modules, delete all directories and reinstall, but still facing the issue not being able to understand the problem
Also encountering similar stuff but with Get-MgUser
I suspect this may be a MS Graph SDK issue and not necessarily an Entra PowerShell module issue.
Whatever the cause, there are several closed issues in the Graph SDK PowerShell repo that point to purging the module[s] as a solution.
- https://github.com/microsoftgraph/msgraph-sdk-powershell/issues/2745#issuecomment-2130127078
- https://github.com/microsoftgraph/msgraph-sdk-powershell/issues/2174#issuecomment-1649686374
- https://github.com/microsoftgraph/msgraph-sdk-powershell/issues/1196#issuecomment-1366701105
These aren't as valuable as a real root cause analysis that finds and fixes the actual problem, but I'm curious if it may help address the symptoms described in this issue.
I suspect this may be a MS Graph SDK issue and not necessarily an Entra PowerShell module issue.
Whatever the cause, there are several closed issues in the Graph SDK PowerShell repo that point to purging the module[s] as a solution.
- get-mguser cmdlet is not working msgraph-sdk-powershell#2745 (comment)
- Commands fail when issuing commands from both versions in the open session. msgraph-sdk-powershell#2174 (comment)
- Issue with Get-MgGroup on PowerShell 5.1 msgraph-sdk-powershell#1196 (comment)
These aren't as valuable as a real root cause analysis that finds and fixes the actual problem, but I'm curious if it may help address the symptoms described in this issue.
Thanks Sam. I was able to resolve my issue loading Microsoft.Graph.Users by upgrading the module.
This is a common issue with PowerShell modules that uses different versions of the same assembly. If you do not address this when designing the module, it will not be usable with other modules, unless you are very careful loading one module before the other to get the newest assembly loaded (often Authentication is the culprit).
The solution is called Assembly Load Contexts Bridging (ALC). I brought this up in the PnP.PowerShell project, where they implemented it and it solved a lot of these assembly load issues: https://github.com/pnp/powershell/issues/3207
ALC is the "new" way of loading assemblies in PowerShell 7, and should have better ways of handling multiple versions of the same assembly. There are a few samples in the code below that show how to avoid this breaking code into two modules and using the IModuleAssemblyInitializer to lazy load and undload assemblies into ALC.
https://learn.microsoft.com/en-us/powershell/scripting/dev-cross-plat/resolving-dependency-conflicts?view=powershell-7.3#more-robust-solutions
Since this module is still very young, this would be the perfect time to adapt ALC Bridging pattern to the code!
Thanks for the tip! Is there any functional equivalent that is backwards compatible with Windows PowerShell 5.1?
Related issue/solution: https://github.com/microsoftgraph/entra-powershell/issues/1242
Closing this since we are tracking #1242
👀 Does this mean you're planning on implementing ACL for when the module is imported in a PowerShell [7] session, @KenitoInc? 👍
This is very annoying, please fix soon
> Import-Module Microsoft.Graph
Import-Module: Assembly with same name is already loaded
This is very annoying, please fix soon
> Import-Module Microsoft.Graph Import-Module: Assembly with same name is already loaded
Indeed. I am trying to update all of my code to run in PS 7 and this is stopping that work.
FWIW, i uninstalled ALL microsoft.graph related modules in powershell 7
(check with get-module -listavailable)
then in your Microsoft.Powershell_profile.ps1 you put
Write-Host 'overriding $PSModulePath'
$Env:PSModulePath = [string]::Join(";",
@(
"C:\\Users\\username\\Path\\To\\Documents\\PowerShell\\Modules",
"C:\\program files\\powershell\\7\\Modules"
))
which overrides the default path which might have legacy powershell modules inside it.
now open a new shell, and ensure you see the above 'overriding...' message
then check again what modules you have with
get-module -listavailable
then reinstall fresh the Microsoft.Graph module and ensure no other modules are there beforehand
after this, i no longer got that error
👀 Does this mean you're planning on implementing ACL for when the module is imported in a PowerShell [7] session, @KenitoInc? 👍
Correct