.net - 使用 WinSCP .NET 的方法未找到异常 (EventWaitHandle..ctor)

标签 .net powershell sftp winscp winscp-net

我正在尝试使用 PowerShell 并使用 WinSCP .NET 程序集连接到 SFTP 服务器。

代码无法打开 session ( $session.Open($sessionOptions) )。

在我发现的日志中,

Exception: System.MissingMethodException: Method not found: 'Void System.Threading.EventWaitHandle..ctor(Boolean, System.Threading.EventResetMode, System.String, Boolean ByRef, System.Security.AccessControl.EventWaitHandleSecurity)'. at WinSCP.ExeSessionProcess.TryCreateEvent(String name, EventWaitHandle& ev) at WinSCP.ExeSessionProcess.InitializeConsole() at WinSCP.ExeSessionProcess.Start() at WinSCP.Session.Open(SessionOptions sessionOptions) [2019-03-14 10:59:02.835Z] [0013] Session.Cleanup entering [2019-03-14 10:59:02.835Z] [0013] Terminating process



基本上,它是从 $session.Open($sessionOptions) 跳转的至 $session.Dispose()并跳过代码的其他部分。

我试图在 WinSCP 论坛上找到解决方案,但没有帮助:
https://winscp.net/forum/viewtopic.php?t=26140
try {
    Add-Type -Path "M:\AMA\ztemp_powershell\WinSCPnet.dll" 

    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "abc.xyz.ca"
        UserName = "abc_abc"
        Password = "*********"
        SshHostKeyFingerprint = "ssh-rsa 2048 **********************"
        PortNumber = 22
    }
    $session = New-Object WinSCP.Session   
    $session.ExecutablePath = "H:\FromLocal\Powershell\WinSCP.exe" 
    $filelist = Get-ChildItem M:\AMA\ztemp_powershell\sample_files

    try {
        # $session.DebugLogPath = "M:\AMA\ztemp_powershell\sftp1.log"
        $session.Open($sessionOptions)

        $transferOptions = New-Object WinSCP.TransferOptions
        $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary

        foreach ($file in $filelist) {
            $transferResult =  $session.PutFiles("M:\AMA\ztemp_powershell\sample_files\$file" , "/outbox/", $false, $transferOptions)

            foreach ($transfer in $transferResult.Transfers) {
                Write-Host "Upload of $($transfer.FileName) succeeded"
            }
        }
    }
    finally {
        $session.Dispose()
    }
    exit 0
}
catch {
    Write-Host "Error: $_"
    exit 1
}

最佳答案

您很可能正在使用 PowerShell 6 或更新版本(又名 PowerShell Core)。这些版本的 PowerShell 使用 .NET (Core),而不是 .NET Framework。
因此,您必须使用 WinSCP .NET 程序集的 .NET Standard 版本。内 WinSCP-*-Automation.zip package ,查看 netstandard2.0文件夹。
阅读 Installing the assembly for PowerShell .

关于.net - 使用 WinSCP .NET 的方法未找到异常 (EventWaitHandle..ctor),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55168750/

相关文章:

windows - Powershell - 关联/导入证书与 RD 网关

powershell - 如何从 PowerShell 中的 SFTP 服务器删除早于 x 天的文件

.net - 确保您的软件和代码的质量

powershell - 检查远程计算机上文件中的文本

.net - dotNet 中是否有 DirectoryInfo.GetFiles/Directory.GetDirectories 的异步版本?

powershell - 实现 PowerShell PSProvider *in* PowerShell

python - SFTP 通过 Paramiko : Errno 10061 (probably proxy)

java - 使用 JSch 和 GlobalScape 的 SFTP

c# - 使用 .Net HttpListener 的多线程

c# - 如何在 C# 控制台应用程序中将修饰符(ctrl、alt、shift)按键作为单个按键捕获?