darkoperator / Posh-SSH

PowerShell Module for automating tasks on remote systems using SSH
BSD 3-Clause "New" or "Revised" License
985 stars 227 forks source link

Assembly with same name is already loaded #595

Open DarkLite1 opened 2 months ago

DarkLite1 commented 2 months ago

Since updating to 3.2.4 we often see this error:

The 'New-SFTPSession' command was found in the module 'Posh-SSH', 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 Posh-SSH'.

We run code in parallel:

@('path1','path2','path3')  | ForEach-Object -Parallel {
    New-SFTPSession -ComputerName 'PC1' -Credential $cred
}

This happens quite often. Is there something we're missing?

We never use Import-Module and rely on auto loading of modules.

This happens on PowerShell 7.4.5. We had to revert back to 3.2.1 where we don't have this issue.

darkoperator commented 2 months ago

In the $error[0] variable does it provide more info on what assembly is causing the issue?

DarkLite1 commented 1 month ago

With the older version (3.2.1) we get this error, but not so often as the error in the OP on the newer version:

The 'New-SFTPSession' command was found in the module 'Posh-SSH', but the module could not be loaded. For more information, run 'Import-Module Posh-SSH'.

It's really hard to troubleshoot as it happens only a couple of times a day. It's probably an issue that happens when loading the assembly at the time the module is loaded from memory that makes it fail to load the New-SFTPSession CmdLet.

Maybe you can add more detailed error handling around the loading of the assemblies? So it spits out more details?

DarkLite1 commented 1 month ago

It happens after this line:

VERBOSE: Loading module from path 'C:\Program Files\PowerShell\Modules\Posh-SSH\3.2.1\PoshSSH.dll'

When I run this code, the error shows up again:

$modulePath = 'C:\Program Files\PowerShell\Modules\Posh-SSH\3.2.1'
Add-Type -Path "$modulePath/PoshSSH.dll"

This code is included within in the module manifest file Posh-SSH.psd1:

# List of all files packaged with this module
FileList = @('Posh-SSH.psm1','PoshSSH.dll','Assembly\Newtonsoft.Json.dll','Assembly\Renci.SshNet.dll', 'Assembly\SshNet.Security.Cryptography.dll')

Is the PoshSSH.dll automatically lauded by PowerShell itself? Could the issue be within the DLL file? That it doesn't check whether it is already loaded or not, before loading again?

Looking at issue #284 it seems to be a bit related. However, running the code below does not generate an error at all:

$modulePath = 'C:\Program Files\PowerShell\Modules\Posh-SSH\3.2.1'
@(
    'Assembly\Newtonsoft.Json.dll',
    'Assembly\Renci.SshNet.dll',
    'Assembly\SshNet.Security.Cryptography.dll'
).ForEach(
    { Add-Type -Path "$modulePath/$_" }
)

image

image

image