Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import-Module : Could not load file or assembly Microsoft.Bcl.AsyncInterfaces #558

Closed
fullenw1 opened this issue Jan 4, 2024 · 40 comments
Closed

Comments

@fullenw1
Copy link

fullenw1 commented Jan 4, 2024

I have this error only on PowerShell Core and not on Windows PowerShell 5.1
It was working fine until the latest release...

Import-Module: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

Here is my PowerShell configuration:

PS> $psversionTable

Name                           Value
----                           -----
PSVersion                      7.4.0
PSEdition                      Core
GitCommitId                    7.4.0
OS                             Microsoft Windows 10.0.19045
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
@doanduyhai
Copy link

doanduyhai commented Jan 4, 2024

I confirm, we face the same issue on Linux (Ubuntu 20.04 LTS)

FileLoadException: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

Tested with

  • Posh-SSH v 3.1.1
  • Posh-SSH v 3.1.2
  • Powershell Core 7.4.0
  • Powershell Core 7.5.0

@darkoperator
Copy link
Owner

darkoperator commented Jan 4, 2024 via email

@doanduyhai
Copy link

When you look at the $error what does the loadexception says? Sent from my iPhoneOn Jan 4, 2024, at 6:12 AM, DuyHai DOAN @.> wrote: I confirm, we face the same issue FileLoadException: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. Tested with Posh-SSH v 3.1.1 Posh-SSH v 3.1.2 Powershell Core 7.4.0 Powershell Core 7.5.0 —Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you are subscribed to this thread.Message ID: @.>

Here is my test code

Write-Host "*** Installing Posh SSH Module 3.1.2 ***"
Install-Module -Name Posh-SSH -Force -RequiredVersion 3.1.2
Write-Host "*** Import Posh SSH Module ***"
try {
  Import-Module Posh-SSH
} catch {
  Write-Host "error = $($Error | Out-String)"
}

And here is the display

*** Installing Posh SSH Module 3.1.2 ***
*** Import Posh SSH Module ***
error = Import-Module: /home/docker/actions-runner/_work/_temp/55116c9b-0848-4837-ba77-fa8c3aa0f3ee.ps1:17
Line |
17 | Import-Module Posh-SSH
| ~~~~~~~~~~~~~~~~~~~~~~
| Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces,
| Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

@darkoperator
Copy link
Owner

I unlisted the module. It seems the last SSH.Net update added a dependency, so I can make it only work with one or the other at the moment. sshnet/SSH.NET#1287

@Rob-Hague
Copy link

(I know nothing about powershell modules) Is it possible to package Microsoft.Bcl.AsyncInterfaces.dll alongside Renci.SshNet.dll?

@doanduyhai
Copy link

I unlisted the module. It seems the last SSH.Net update added a dependency, so I can make it only work with one or the other at the moment. sshnet/SSH.NET#1287

Will you do a new release for the fix ?

Regards

@darkoperator
Copy link
Owner

darkoperator commented Jan 4, 2024 via email

@darkoperator
Copy link
Owner

I unlisted the module. It seems the last SSH.Net update added a dependency, so I can make it only work with one or the other at the moment. sshnet/SSH.NET#1287

Will you do a new release for the fix ?

Regards

If I can fix it, seems the dependency messes stuff up when running under .ner standard 2.0

╭─ pwsh  ~\Documents\GitHub\Posh-SSH\Source\PoshSSH\PoshSSH.Core\bin\Release\netstandard2.0   master ?9 ~1                        09:51:58 
╰─❯ New-SSHSession v0.local pi

PowerShell credential request
Enter your credentials.
Password for user pi: ********

New-SSHSession: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. Could not find or load a specific file. (0x80131621)
╭─ pwsh  ~\Documents\GitHub\Posh-SSH\Source\PoshSSH\PoshSSH.Core\bin\Release\netstandard2.0   master ?9 ~1                        09:53:15 
╰─❯ ls

        Directory: C:\Users\Carlos Perez\Documents\GitHub\Posh-SSH\Source\PoshSSH\PoshSSH.Core\bin\Release\netstandard2.0


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a---        10/31/2023  11:00 AM          26784   Microsoft.Bcl.AsyncInterfaces.dll
-a---          1/4/2024   9:44 AM           5439   PoshSSH.deps.json
-a---          1/4/2024   9:44 AM          46080   PoshSSH.dll
-a---          1/4/2024   9:44 AM          22320   PoshSSH.pdb

╭─ pwsh  ~\Documents\GitHub\Posh-SSH\Source\PoshSSH\PoshSSH.Core\bin\Release\netstandard2.0   master ?9 ~1                        09:53:19 
╰─❯ add-type -Path .\microsoft.Bcl.AsyncInterfaces.dll
Add-Type: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

@darkoperator
Copy link
Owner

I copied the files straight from the .Netstandard 2.0 nugget for the library it will not even let me load it by hand

@Rob-Hague
Copy link

Where is Renci.SshNet.dll and SshNet.Security.Cryptography.dll? Should Microsoft.Bcl.AsyncInterfaces.dll be alongside those?

@darkoperator
Copy link
Owner

all of the files from the dependecies json file

─ pwsh  ~\Documents\GitHub\Posh-SSH\Posh-SSH   master ?9 ~1                                                                      10:10:09 
╰─❯ ls

        Directory: C:\Users\Carlos Perez\Documents\GitHub\Posh-SSH\Posh-SSH


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d----          1/4/2024   9:35 AM                  Assembly
d----        10/22/2023   1:56 PM                  en-US
d----          6/2/2021   5:35 PM                  Format
-a---        10/31/2023  11:00 AM          26784   Microsoft.Bcl.AsyncInterfaces.dll
-a---         3/17/2021   8:03 PM         701992   Newtonsoft.Json.dll
-a---          1/4/2024   9:37 AM           5342 󰞷  Posh-SSH.psd1
-a---        10/23/2023   7:40 AM          93572 󰞷  Posh-SSH.psm1
-a---        12/31/2023   9:37 AM          46080   PoshSSH.dll
-a---        12/29/2023   9:57 AM         839168   Renci.SshNet.dll
-a---        10/22/2017   8:53 PM          34816   SshNet.Security.Cryptography.dll
-a---         1/25/2021   2:43 PM          20856   System.Buffers.dll
-a---         12/2/2020   4:59 AM        1608160   System.Management.Automation.dll
-a---         1/25/2021   2:43 PM         141184   System.Memory.dll
-a---         1/25/2021   2:43 PM         115856   System.Numerics.Vectors.dll
-a---         2/19/2020   6:05 AM          25984   System.Threading.Tasks.Extensions.dll

╭─ pwsh  ~\Documents\GitHub\Posh-SSH\Posh-SSH   master ?9 ~1                                                                      10:10:11 
╰─❯ add-type -Path .\microsoft.Bcl.AsyncInterfaces.dll
Add-Type: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
╭─ pwsh  ~\Documents\GitHub\Posh-SSH\Posh-SSH   master ?9 ~1                                                                      10:10:21 
╰─❯

@darkoperator
Copy link
Owner

the .net standard 2.0 files load no problems under PS version 5.1

PS C:\Users\Carlos Perez> cd "C:\Users\Carlos Perez\Documents\GitHub\Posh-SSH\Posh-SSH"
PS C:\Users\Carlos Perez\Documents\GitHub\Posh-SSH\Posh-SSH> add-type -Path .\microsoft.Bcl.AsyncInterfaces.dll
PS C:\Users\Carlos Perez\Documents\GitHub\Posh-SSH\Posh-SSH>

@Rob-Hague
Copy link

Any more error details from $error.Exception?

@darkoperator
Copy link
Owner

$error[0].exception | fl *

Message        : Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=8.0.0.0, Culture=neutral,
                 PublicKeyToken=cc7b13ffcd2ddd51'.
FileName       :
FusionLog      :
TargetSite     : System.Reflection.Assembly LoadFromAssemblyPath(System.String)
Data           : {}
InnerException :
HelpLink       :
Source         : System.Private.CoreLib
HResult        : -2146232799
StackTrace     :    at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(String assemblyPath)
                    at System.Reflection.Assembly.LoadFrom(String assemblyFile)
                    at Microsoft.PowerShell.Commands.AddTypeCommand.LoadAssemblies(IEnumerable`1 assemblies)
                    at System.Management.Automation.CommandProcessorBase.Complete()

@Rob-Hague
Copy link

The HResult is 0x80131621 which has a few results on google, including pnp/powershell#1814

Perhaps there is some conflict somewhere. Does this show anything?

[System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object {$_.FullName -match "Microsoft.Bcl.AsyncInterfaces"} | Select-Object -Property FullName, Location | fl

@darkoperator
Copy link
Owner

darkoperator commented Jan 4, 2024

Tried it at startup, after loading SSH.NET, after importing the module. Seems to error when it is actually used. DLL is in the same directory

╭─ pwsh  ~\Documents\GitHub\Posh-SSH\Posh-SSH   master ?9 ~1                                                                      10:10:21 
╰─❯ [System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object {$_.FullName -match "Microsoft.Bcl.AsyncInterfaces"} | Select-Object -Property
 FullName, Location | fl
╭─ pwsh  ~\Documents\GitHub\Posh-SSH\Posh-SSH   master ?9 ~1                                                                      11:14:26 
╰─❯ Add-Type -Path .\Renci.SshNet.dll
╭─ pwsh  ~\Documents\GitHub\Posh-SSH\Posh-SSH   master ?9 ~1                                                                      11:15:01 
╰─❯ [System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object {$_.FullName -match "Microsoft.Bcl.AsyncInterfaces"} | Select-Object -Property
 FullName, Location | fl
╭─ pwsh  ~\Documents\GitHub\Posh-SSH\Posh-SSH   master ?9 ~1                                                                      11:15:03 
╰─❯ import-module -verbose .\PoshSSH.dll
VERBOSE: Importing cmdlet 'Get-SSHHostKey'.
VERBOSE: Importing cmdlet 'Get-SCPItem'.
VERBOSE: Importing cmdlet 'Get-SFTPItem'.
VERBOSE: Importing cmdlet 'New-SFTPSession'.
VERBOSE: Importing cmdlet 'New-SSHSession'.
VERBOSE: Importing cmdlet 'Set-SCPItem'.
VERBOSE: Importing cmdlet 'Set-SFTPItem'.
VERBOSE: Importing cmdlet 'New-SSHMemoryKnownHost'.
VERBOSE: Importing cmdlet 'Get-SSHJsonKnownHost'.
VERBOSE: Importing cmdlet 'Get-SSHOpenSSHKnownHost'.
VERBOSE: Importing alias 'Get-SSHJsonKnowHost'.
╭─ pwsh  ~\Documents\GitHub\Posh-SSH\Posh-SSH   master ?9 ~1                                                                      11:15:31 
╰─❯ New-SSHSession v0.local pi

PowerShell credential request
Enter your credentials.
Password for user pi: ********

New-SSHSession: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. Could not find or load a specific file. (0x80131621)
╭─ pwsh  ~\Documents\GitHub\Posh-SSH\Posh-SSH   master ?9 ~1                                                                      11:15:41 
╰─❯ [System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object {$_.FullName -match "Microsoft.Bcl.AsyncInterfaces"} | Select-Object -Property
 FullName, Location | fl
╭─ pwsh  ~\Documents\GitHub\Posh-SSH\Posh-SSH   master ?9 ~1

@darkoperator
Copy link
Owner

I set on the project true to have it bundle all dependencies and still not working on PS7, it has to be some bug or something changed in PS. I did notice that when I look at [System.AppDomain]::CurrentDomain.GetAssemblies() | where {$_.location -like "user"} I see the module is loaded and SSH.Net assembly, but if I look at all assemblies some are blank for some reason

@darkoperator
Copy link
Owner

Now I'm completely lost. I can load it by hand, it is in the CAG but it can not find it .. .

New-SSHSession: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. Could not find or load a specific file. (0x80131621)
╭─ pwsh  ~\Documents\GitHub\Posh-SSH\Posh-SSH   master ?9 ~2                                                                                                                             14:44:30 
╰─❯ [Reflection.Assembly]::LoadFile('C:\Users\Carlos Perez\Documents\GitHub\Posh-SSH\Posh-SSH\Microsoft.Bcl.AsyncInterfaces.dll')

GAC    Version        Location
---    -------        --------
False  v4.0.30319     C:\Users\Carlos Perez\Documents\GitHub\Posh-SSH\Posh-SSH\Microsoft.Bcl.AsyncInterfaces.dll

╭─ pwsh  ~\Documents\GitHub\Posh-SSH\Posh-SSH   master ?9 ~2                                                                                                                             14:44:46 
╰─❯ New-SSHSession v0.local pi

PowerShell credential request
Enter your credentials.
Password for user pi: ********

New-SSHSession: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. Could not find or load a specific file. (0x80131621)

@Rob-Hague
Copy link

I have a C:\Program Files\PowerShell\7\Microsoft.Bcl.AsyncInterfaces.dll which is version 5.0.0.0

The problem could be that the SSH.NET nuspec defines an exact dependency (with square brackets) on 8.0.0 and because PS7 has a dependency on a different version already, it refuses to load another.

You could try the following diff in SSH.NET:

diff --git a/src/Renci.SshNet/Renci.SshNet.csproj b/src/Renci.SshNet/Renci.SshNet.csproj
index 877b3b89..334df508 100644
--- a/src/Renci.SshNet/Renci.SshNet.csproj
+++ b/src/Renci.SshNet/Renci.SshNet.csproj
@@ -14,7 +14,7 @@
   </ItemGroup>

   <ItemGroup Condition=" '$(TargetFramework)' == 'net462' or '$(TargetFramework)' == 'netstandard2.0' ">
-    <PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
+    <PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="1.0.0" /> <!-- or a higher version -->
   </ItemGroup>

   <PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netstandard2.1' or '$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'net7.0' or '$(TargetFramework)' == 'net8.0' ">

and try again with a local build of the library.

If things are still not getting anywhere, I would be in favour of removing the dependency from the library to unblock your module. It is not a necessary part of the library. cc @WojciechNagorski

@darkoperator
Copy link
Owner

@Rob-Hague Thanks!! it worked now on both versions of PS, I will do more tests before I update my module

@Rob-Hague
Copy link

Great

It would be useful if you could do a quick check with a local build using version 8.0.0 and see whether it still works (which would imply the problem would be fixed by sshnet/SSH.NET#1288), or if it only works with a version <= 5.0.0 or whatever PS7 seems to be using (in which case, we should probably remove the dependency altogether)

@mus65
Copy link

mus65 commented Jan 5, 2024

@darkoperator @Rob-Hague I'm confused. If the csproj diff fixes it, wouldn't this mean the problem is version 8.0.0? In this case sshnet/SSH.NET#1288 wouldn't fix it, since SSH.NET would still need 8.0.0 (it would just also accept 9.0 etc.).

If PowerShell can't handle the newer Microsoft.Bcl.AsyncInterfaces Version for whatever reason, it would probably be best for SSH.NET to require >=1.0.0 instead >=8.0.0 . I doubt that SSH.NET actually needs an up-to-date version since it's just a compat package for .NET Framework / .NET Standard.

@darkoperator
Copy link
Owner

yep, with version 1.1.0 it worked in terms of loading and establishing a SSH connection. Version 8.0.0 loading was the version causing the problem in PS 7

@WojciechNagorski
Copy link

Can you prepare a PR?

@darkoperator
Copy link
Owner

I think this one is covering it sshnet/SSH.NET#1288

@Rob-Hague
Copy link

@mus65 I'm not so clear on the nuances of dll loading 🙂

Can you can modify your PR to take version >= 1.0.0?

mus65 added a commit to mus65/SSH.NET that referenced this issue Jan 5, 2024
This technically fixes 2 different issues:
- previously SSH.NET would require exactly(!) version 8.0.0, which would
  cause issues in the future if e.g. a project using SSH.NET would
  try to update to version 9.0.0. In this case the constraint on the
  exact version would cause NuGet to refuse to upgrade to 9.0.0 (sshnet#1287)
- PowerShell Core seems to have problems with Version 8.0.0, therefore
  downgrade the requirement to 1.0.0 (see darkoperator/Posh-SSH#558)
@mus65
Copy link

mus65 commented Jan 5, 2024

@WojciechNagorski @Rob-Hague @darkoperator I updated my PR to fix both issues: sshnet/SSH.NET#1288

mus65 added a commit to mus65/SSH.NET that referenced this issue Jan 5, 2024
This technically fixes 2 different issues:
- previously SSH.NET would require exactly(!) version 8.0.0, which would
  cause issues in the future if e.g. a project using SSH.NET would
  try to update to version 9.0.0. In this case the constraint on the
  exact version would cause NuGet to refuse to upgrade to 9.0.0 (sshnet#1287)
- PowerShell Core seems to have problems with Version 8.0.0, therefore
  downgrade the requirement to 1.0.0 (see darkoperator/Posh-SSH#558)

I had to re-enable AutoGenerateBindingRedirects, otherwise there is a
FileLoadException with net462 in one of the tests. I don't know why this
was disabled in 3ecbd10 .
WojciechNagorski pushed a commit to sshnet/SSH.NET that referenced this issue Jan 5, 2024
This technically fixes 2 different issues:
- previously SSH.NET would require exactly(!) version 8.0.0, which would
  cause issues in the future if e.g. a project using SSH.NET would
  try to update to version 9.0.0. In this case the constraint on the
  exact version would cause NuGet to refuse to upgrade to 9.0.0 (#1287)
- PowerShell Core seems to have problems with Version 8.0.0, therefore
  downgrade the requirement to 1.0.0 (see darkoperator/Posh-SSH#558)

I had to re-enable AutoGenerateBindingRedirects, otherwise there is a
FileLoadException with net462 in one of the tests. I don't know why this
was disabled in 3ecbd10 .
@fMichaleczek
Copy link

@darkoperator PowerShell distributes Microsoft.Bcl.AsyncInterfaces, so if you want to load another version, that creates a conflict. Every time the PowerShell team changes the version of this assembly, it will break your module.

The technical explanation is here:
https://learn.microsoft.com/en-us/powershell/scripting/dev-cross-plat/resolving-dependency-conflicts?view=powershell-7.4

In the quick version (this is contained in the previous document), if you define a class that implements IModuleAssemblyInitializer , when the module loads, you can insert code that allows you to load a library with the version of your choice (via an AssemblyLoadContext)

I'm giving you the repository of the person who worked on the subject at the time (same use case as the doc)
https://github.com/rjmholt/ModuleDependencyIsolationExample/tree/master

A random example : https://github.com/PSBicep/BicepNet/blob/main/BicepNet.PS/LoadContext/ModuleInitializer.cs

@doanduyhai
Copy link

Hello @darkoperator

Any update on the fix ? Should I use the latest release of the module ?

@darkoperator
Copy link
Owner

darkoperator commented Jan 18, 2024 via email

@darkoperator
Copy link
Owner

@doanduyhai can you give the pre-release a test https://github.com/darkoperator/Posh-SSH/releases/tag/3.1.3-Beta1

@doanduyhai
Copy link

Hello @darkoperator

I'll be happy to test the Beta release

However, because I'm a newbie in Powershell and .Net environment, I don't know how to use your beta package in .zip release form

Usually, to install a Powershell module, I just use the command Install-Module -Name Posh-SSH

Please give me instructions on how to deploy the zip file and I'll be happy to test it

Regards

@darkoperator
Copy link
Owner

darkoperator commented Jan 22, 2024 via email

@doanduyhai
Copy link

@darkoperator

Here is what I get

Source code:

          Write-Host "****** Loading Posh-SSH beta version ********"
          Import-Module -Name /_redacted_/posh-ssh-binaries/Posh-SSH -Verbose

Result:

****** Loading Posh-SSH beta version ********
VERBOSE: Loading module from path '/_redacted_/posh-ssh-binaries/Posh-SSH/Posh-SSH.psd1'.
VERBOSE: Loading 'FormatsToProcess' from path '/_redacted_/posh-ssh-binaries/Posh-SSH/Format/SSHSession.Format.ps1xml'.
VERBOSE: Loading 'FormatsToProcess' from path '/_redacted_/posh-ssh-binaries/Posh-SSH/Format/SFTPSession.Format.ps1xml'.
VERBOSE: Loading 'FormatsToProcess' from path '/_redacted_/posh-ssh-binaries/Posh-SSH/Format/Renci.SshNet.SshCommand.Format.ps1xml'.
VERBOSE: Loading 'FormatsToProcess' from path '/_redacted_/posh-ssh-binaries/Posh-SSH/Format/Renci.SshNet.Sftp.SftpFile.Format.ps1xml'.
VERBOSE: Loading module from path '/_redacted_/posh-ssh-binaries/Posh-SSH/PoshSSH.dll'.
VERBOSE: Importing cmdlet 'Get-SSHHostKey'.
VERBOSE: Importing cmdlet 'Get-SCPItem'.
VERBOSE: Importing cmdlet 'Get-SFTPItem'.
VERBOSE: Importing cmdlet 'New-SFTPSession'.
VERBOSE: Importing cmdlet 'New-SSHSession'.
VERBOSE: Importing cmdlet 'Set-SCPItem'.
VERBOSE: Importing cmdlet 'Set-SFTPItem'.
VERBOSE: Importing cmdlet 'New-SSHMemoryKnownHost'.
VERBOSE: Importing cmdlet 'Get-SSHJsonKnownHost'.
VERBOSE: Importing cmdlet 'Get-SSHOpenSSHKnownHost'.
VERBOSE: Importing alias 'Get-SSHJsonKnowHost'.
VERBOSE: Loading module from path '/home/docker4sg/actions-runner/_work/test_repo1/test_repo1/posh-ssh-binaries/Posh-SSH/Posh-SSH.psm1'.
VERBOSE: Exporting function 'Get-SSHSession'.
VERBOSE: Exporting function 'Remove-SSHSession'.
VERBOSE: Exporting function 'Invoke-SSHCommand'.
VERBOSE: Exporting function 'Get-PoshSSHModVersion'.
VERBOSE: Exporting function 'Invoke-SSHCommandStream'.
VERBOSE: Exporting function 'New-SSHShellStream'.
VERBOSE: Exporting function 'Invoke-SSHStreamExpectAction'.
VERBOSE: Exporting function 'Invoke-SSHStreamExpectSecureAction'.
VERBOSE: Exporting function 'Invoke-SSHStreamShellCommand'.
VERBOSE: Exporting function 'Get-SFTPSession'.
VERBOSE: Exporting function 'Remove-SFTPSession'.
VERBOSE: Exporting function 'Get-SFTPChildItem'.
VERBOSE: Exporting function 'Test-SFTPPath'.
VERBOSE: Exporting function 'Remove-SFTPItem'.
VERBOSE: Exporting function 'Move-SFTPItem'.
VERBOSE: Exporting function 'Set-SFTPLocation'.
VERBOSE: Exporting function 'Get-SFTPLocation'.
VERBOSE: Exporting function 'Rename-SFTPFile'.
VERBOSE: Exporting function 'Get-SFTPPathAttribute'.
VERBOSE: Exporting function 'Set-SFTPPathAttribute'.
VERBOSE: Exporting function 'Get-SFTPPathInformation'.
VERBOSE: Exporting function 'New-SFTPSymlink'.
VERBOSE: Exporting function 'Get-SFTPContent'.
VERBOSE: Exporting function 'Set-SFTPContent'.
VERBOSE: Exporting function 'New-SFTPFileStream'.
VERBOSE: Exporting function 'New-SFTPItem'.
VERBOSE: Exporting function 'New-SSHLocalPortForward'.
VERBOSE: Exporting function 'New-SSHRemotePortForward'.
VERBOSE: Exporting function 'New-SSHDynamicPortForward'.
VERBOSE: Exporting function 'Get-SSHPortForward'.
VERBOSE: Exporting function 'Stop-SSHPortForward'.
VERBOSE: Exporting function 'Start-SSHPortForward'.
VERBOSE: Exporting function 'Get-SSHTrustedHost'.
VERBOSE: Exporting function 'New-SSHTrustedHost'.
VERBOSE: Exporting function 'Remove-SSHTrustedHost'.
VERBOSE: Exporting function 'Get-SSHRegistryKnownHost'.
VERBOSE: Exporting function 'Convert-SSHRegistryToJSonKnownHost'.
VERBOSE: Exporting alias 'Get-SSHJsonKnowHost'.
VERBOSE: Importing function 'Convert-SSHRegistryToJSonKnownHost'.
VERBOSE: Importing function 'Get-PoshSSHModVersion'.
VERBOSE: Importing function 'Get-SFTPChildItem'.
VERBOSE: Importing function 'Get-SFTPContent'.
VERBOSE: Importing function 'Get-SFTPLocation'.
VERBOSE: Importing function 'Get-SFTPPathAttribute'.
VERBOSE: Importing function 'Get-SFTPPathInformation'.
VERBOSE: Importing function 'Get-SFTPSession'.
VERBOSE: Importing function 'Get-SSHPortForward'.
VERBOSE: Importing function 'Get-SSHRegistryKnownHost'.
VERBOSE: Importing function 'Get-SSHSession'.
VERBOSE: Importing function 'Get-SSHTrustedHost'.
VERBOSE: Importing function 'Invoke-SSHCommand'.
VERBOSE: Importing function 'Invoke-SSHCommandStream'.
VERBOSE: Importing function 'Invoke-SSHStreamExpectAction'.
VERBOSE: Importing function 'Invoke-SSHStreamExpectSecureAction'.
VERBOSE: Importing function 'Invoke-SSHStreamShellCommand'.
VERBOSE: Importing function 'Move-SFTPItem'.
VERBOSE: Importing function 'New-SFTPFileStream'.
VERBOSE: Importing function 'New-SFTPItem'.
VERBOSE: Importing function 'New-SFTPSymlink'.
VERBOSE: Importing function 'New-SSHDynamicPortForward'.
VERBOSE: Importing function 'New-SSHLocalPortForward'.
VERBOSE: Importing function 'New-SSHRemotePortForward'.
VERBOSE: Importing function 'New-SSHShellStream'.
VERBOSE: Importing function 'New-SSHTrustedHost'.
VERBOSE: Importing function 'Remove-SFTPItem'.
VERBOSE: Importing function 'Remove-SFTPSession'.
VERBOSE: Importing function 'Remove-SSHSession'.
VERBOSE: Importing function 'Remove-SSHTrustedHost'.
VERBOSE: Importing function 'Rename-SFTPFile'.
VERBOSE: Importing function 'Set-SFTPContent'.
VERBOSE: Importing function 'Set-SFTPLocation'.
VERBOSE: Importing function 'Set-SFTPPathAttribute'.
VERBOSE: Importing function 'Start-SSHPortForward'.
VERBOSE: Importing function 'Stop-SSHPortForward'.
VERBOSE: Importing function 'Test-SFTPPath'.
VERBOSE: Importing alias 'Get-SSHJsonKnowHost'.
VERBOSE: Exporting function 'Convert-SSHRegistryToJSonKnownHost'.
VERBOSE: Exporting function 'Get-PoshSSHModVersion'.
VERBOSE: Exporting function 'Get-SFTPChildItem'.
VERBOSE: Exporting function 'Get-SFTPContent'.
VERBOSE: Exporting function 'Get-SFTPLocation'.
VERBOSE: Exporting function 'Get-SFTPPathAttribute'.
VERBOSE: Exporting function 'Get-SFTPPathInformation'.
VERBOSE: Exporting function 'Get-SFTPSession'.
VERBOSE: Exporting function 'Get-SSHPortForward'.
VERBOSE: Exporting function 'Get-SSHRegistryKnownHost'.
VERBOSE: Exporting function 'Get-SSHSession'.
VERBOSE: Exporting function 'Get-SSHTrustedHost'.
VERBOSE: Exporting function 'Invoke-SSHCommand'.
VERBOSE: Exporting function 'Invoke-SSHCommandStream'.
VERBOSE: Exporting function 'Invoke-SSHStreamExpectAction'.
VERBOSE: Exporting function 'Invoke-SSHStreamExpectSecureAction'.
VERBOSE: Exporting function 'Invoke-SSHStreamShellCommand'.
VERBOSE: Exporting function 'Move-SFTPItem'.
VERBOSE: Exporting function 'New-SFTPFileStream'.
VERBOSE: Exporting function 'New-SFTPItem'.
VERBOSE: Exporting function 'New-SFTPSymlink'.
VERBOSE: Exporting function 'New-SSHDynamicPortForward'.
VERBOSE: Exporting function 'New-SSHLocalPortForward'.
VERBOSE: Exporting function 'New-SSHRemotePortForward'.
VERBOSE: Exporting function 'New-SSHShellStream'.
VERBOSE: Exporting function 'New-SSHTrustedHost'.
VERBOSE: Exporting function 'Remove-SFTPItem'.
VERBOSE: Exporting function 'Remove-SFTPSession'.
VERBOSE: Exporting function 'Remove-SSHSession'.
VERBOSE: Exporting function 'Remove-SSHTrustedHost'.
VERBOSE: Exporting function 'Rename-SFTPFile'.
VERBOSE: Exporting function 'Set-SFTPContent'.
VERBOSE: Exporting function 'Set-SFTPLocation'.
VERBOSE: Exporting function 'Set-SFTPPathAttribute'.
VERBOSE: Exporting function 'Start-SSHPortForward'.
VERBOSE: Exporting function 'Stop-SSHPortForward'.
VERBOSE: Exporting function 'Test-SFTPPath'.
VERBOSE: Exporting cmdlet 'Get-SSHHostKey'.
VERBOSE: Exporting cmdlet 'Get-SCPItem'.
VERBOSE: Exporting cmdlet 'Get-SFTPItem'.
VERBOSE: Exporting cmdlet 'New-SFTPSession'.
VERBOSE: Exporting cmdlet 'New-SSHSession'.
VERBOSE: Exporting cmdlet 'Set-SCPItem'.
VERBOSE: Exporting cmdlet 'Set-SFTPItem'.
VERBOSE: Exporting cmdlet 'New-SSHMemoryKnownHost'.
VERBOSE: Exporting cmdlet 'Get-SSHJsonKnownHost'.
VERBOSE: Exporting cmdlet 'Get-SSHOpenSSHKnownHost'.
VERBOSE: Exporting alias 'Get-SSHJsonKnowHost'.
VERBOSE: Importing cmdlet 'Get-SCPItem'.
VERBOSE: Importing cmdlet 'Get-SFTPItem'.
VERBOSE: Importing cmdlet 'Get-SSHHostKey'.
VERBOSE: Importing cmdlet 'Get-SSHJsonKnownHost'.
VERBOSE: Importing cmdlet 'Get-SSHOpenSSHKnownHost'.
VERBOSE: Importing cmdlet 'New-SFTPSession'.
VERBOSE: Importing cmdlet 'New-SSHMemoryKnownHost'.
VERBOSE: Importing cmdlet 'New-SSHSession'.
VERBOSE: Importing cmdlet 'Set-SCPItem'.
VERBOSE: Importing cmdlet 'Set-SFTPItem'.
VERBOSE: Importing function 'Convert-SSHRegistryToJSonKnownHost'.
VERBOSE: Importing function 'Get-PoshSSHModVersion'.
VERBOSE: Importing function 'Get-SFTPChildItem'.
VERBOSE: Importing function 'Get-SFTPContent'.
VERBOSE: Importing function 'Get-SFTPLocation'.
VERBOSE: Importing function 'Get-SFTPPathAttribute'.
VERBOSE: Importing function 'Get-SFTPPathInformation'.
VERBOSE: Importing function 'Get-SFTPSession'.
VERBOSE: Importing function 'Get-SSHPortForward'.
VERBOSE: Importing function 'Get-SSHRegistryKnownHost'.
VERBOSE: Importing function 'Get-SSHSession'.
VERBOSE: Importing function 'Get-SSHTrustedHost'.
VERBOSE: Importing function 'Invoke-SSHCommand'.
VERBOSE: Importing function 'Invoke-SSHCommandStream'.
VERBOSE: Importing function 'Invoke-SSHStreamExpectAction'.
VERBOSE: Importing function 'Invoke-SSHStreamExpectSecureAction'.
VERBOSE: Importing function 'Invoke-SSHStreamShellCommand'.
VERBOSE: Importing function 'Move-SFTPItem'.
VERBOSE: Importing function 'New-SFTPFileStream'.
VERBOSE: Importing function 'New-SFTPItem'.
VERBOSE: Importing function 'New-SFTPSymlink'.
VERBOSE: Importing function 'New-SSHDynamicPortForward'.
VERBOSE: Importing function 'New-SSHLocalPortForward'.
VERBOSE: Importing function 'New-SSHRemotePortForward'.
VERBOSE: Importing function 'New-SSHShellStream'.
VERBOSE: Importing function 'New-SSHTrustedHost'.
VERBOSE: Importing function 'Remove-SFTPItem'.
VERBOSE: Importing function 'Remove-SFTPSession'.
VERBOSE: Importing function 'Remove-SSHSession'.
VERBOSE: Importing function 'Remove-SSHTrustedHost'.
VERBOSE: Importing function 'Rename-SFTPFile'.
VERBOSE: Importing function 'Set-SFTPContent'.
VERBOSE: Importing function 'Set-SFTPLocation'.
VERBOSE: Importing function 'Set-SFTPPathAttribute'.
VERBOSE: Importing function 'Start-SSHPortForward'.
VERBOSE: Importing function 'Stop-SSHPortForward'.
VERBOSE: Importing function 'Test-SFTPPath'.
VERBOSE: Importing alias 'Get-SSHJsonKnowHost'.

@doanduyhai
Copy link

Integration test with a real SFTP endpoint

Source:

 Write-Host "Downloading secret 'private-ssh-key' from KeyVault '__redacted__' to local private.pem file"
  az keyvault secret download `
    --vault-name "__redacted__" `
    --name "sftp-private-ssh-key" `
    --file ./private.pem `
    --encoding ascii
  
  $username="__redacted__"
  $password = ""
  $secureStringPwd = $password | ConvertTo-SecureString -AsPlainText -Force
  $credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $secureStringPwd
  
  try {
    $blobEndpoint = "__redacted__"
    Write-Host "Connecting to SFTP endpoint '$blobEndpoint' with user '$username'"
    $currentSession=New-SFTPSession `
      -Computername "$blobEndpoint" `
      -Credential $credential `
      -KeyFile ./private.pem `
      -AcceptKey `
      -Force
  } finally {
    if($currentSession -ne $null) {
        Write-Host "Disconnecting SFTP session from host '$blobEndpoint'"
        Remove-SFTPSession $currentSession
    }
  
    if (Test-Path "./private.pem") {
        Write-Host "Removing local 'private.pem' file"
        Remove-Item "./private.pem"
    }
  }

Result:

Downloading secret 'private-ssh-key' from KeyVault '_redacted_' to local private.pem file
Connecting to SFTP endpoint '_redacted_' with user '_redacted_'
WARNING: Host key is not being verified since Force switch is used.
Disconnecting SFTP session from host '_redacted_'
Removing local 'private.pem' file

@darkoperator
Copy link
Owner

darkoperator commented Jan 22, 2024 via email

@doanduyhai
Copy link

Thanks, was this by any chance against a Cisco device?

No, I was testing against an Azure Storage Account with SFTP feature enabled

@jczinger
Copy link

I was experiencing this issue in an Azure Function running Powershell 7. Setting the required version of this module to the new beta release resolved the issue.

@DarkLite1
Copy link

Is this fixed in version Posh-SSH 3.1.3? That's the current version in the PowerShell gallery but that version is marked here on GitHub as being Beta.

@darkoperator
Copy link
Owner

darkoperator commented Jan 30, 2024 via email

sendevman pushed a commit to sendevman/SSH.NET that referenced this issue Sep 1, 2024
This technically fixes 2 different issues:
- previously SSH.NET would require exactly(!) version 8.0.0, which would
  cause issues in the future if e.g. a project using SSH.NET would
  try to update to version 9.0.0. In this case the constraint on the
  exact version would cause NuGet to refuse to upgrade to 9.0.0 (#1287)
- PowerShell Core seems to have problems with Version 8.0.0, therefore
  downgrade the requirement to 1.0.0 (see darkoperator/Posh-SSH#558)

I had to re-enable AutoGenerateBindingRedirects, otherwise there is a
FileLoadException with net462 in one of the tests. I don't know why this
was disabled in 3ecbd1071d653bbf04254c08d14d7ce75d60688b .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants