Posh-SSH icon indicating copy to clipboard operation
Posh-SSH copied to clipboard

Assembly with same name is already loaded

Open DarkLite1 opened this issue 1 year ago • 3 comments

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.

DarkLite1 avatar Sep 23 '24 07:09 DarkLite1

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

darkoperator avatar Sep 28 '24 11:09 darkoperator

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 avatar Oct 09 '24 09:10 DarkLite1

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

DarkLite1 avatar Oct 09 '24 09:10 DarkLite1