powershell: [BUG] Error with colliding assemblies when using Start-ThreadJob

Reporting an Issue or Missing Feature

Issue

Expected behavior

According to the note in the start of this article: https://pnp.github.io/powershell/articles/azurefunctions.html image We should use Start-ThreadJob as a way to mitigate the assembly load issue that especially now with .NET 6 has been an issue when using PnP.PowerShell with other modules, like Microsoft.Identity.Client in Az.Account

When I try the examples in the above article with Start-Job in PowerShell 5.1 it works fine, but as soon as I use PowerShell 7 it fails with the dreaded “Assembly with same name is already loaded”

I believe this is because the way AppDomains work in .NET Framework vs .NET Core

On a related note: _I know you did a lot to prevent these issues when upgrading to 2.1.1 but did you consider ALC bridging? 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

Would love to hear your thoughts? 😃_

Actual behavior

When using Start-ThreadJob in PowerShell 7 we were expecting to be able to use Az along with PnP.PowerShell

Steps to reproduce behavior

$script = {
      Connect-PnPOnline tenantname.sharepoint.com/sites/test -Interactive
      $web = Get-PnPWeb
      $web.Title}
$webtitle = Start-ThreadJob -ScriptBlock $script | Wait-Job | Receive-Job
# webtitle returns title:
$webtitle
PS> test123

Also a observe that loaded modules show that modules are indeed not loaded, but checking the appdomain for assemblies reveal that pnp powershell are indeed loaded witch will cause trouble later:

[AppDomain]::CurrentDomain.GetAssemblies() | ? FullName -like *pnp.powershell* | select fullname
PS> PnP.PowerShell, Version=2.1.1.0, Culture=neutral, PublicKeyToken=null
PS> PnP.PowerShell.ALC, Version=2.1.1.0, Culture=neutral, PublicKeyToken=null

Now if we try to run an Az command like Get-AzADUser in a seperate job look what happens:

$getUsers = {
    Connect-AzAccount -Tenantid $tenantId -SubscriptionId $subid | out-null
   Get-AzAdUser
}
Start-ThreadJob -ScriptBlock $getUsers | Wait-Job | Receive-Job

The ‘Get-AzADUser’ command was found in the module ‘Az.Resources’, but the module could not be loaded due to the following error: [Assembly with same name is already loaded] For more information, run ‘Import-Module Az.Resources’.

What is the version of the Cmdlet module you are running?

PowerShell 7.3.4 PnP.PowerShell 2.1.1 Az.Resources 3.3.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: closed
  • Created 3 years ago
  • Comments: 18

Commits related to this issue

Most upvoted comments

Hello, I’m using in Azure Automation the module 2.4.0 | PnP.PowerShell and it is still loading Microsoft.Graph.Core, Version=1.25.1.0 so the problem is still there and I could not “Could not load type 'Microsoft.Graph.Authentication.AzureIdentityAccessTokenProvider”. what can I do?

Welcome 🤗

This has been long overdue , spent quite some time over this but looks like I finally managed to get it working.

Let me know if it’s still an issue.

You will need today’s nightly build to test , just tested on my machine and it worked 😊

Also confirmed by me. Works like expected. Thank you for fast resolving.

Welcome 🤗

This has been long overdue , spent quite some time over this but looks like I finally managed to get it working.

Let me know if it’s still an issue.

You will need today’s nightly build to test , just tested on my machine and it worked 😊

Very nice job @gautamdsheth !

I just tested connecting to SPO and connecting to Az in the same session without issue.

This just made my day THANK YOU!