为什么我得到 "SessionOptions.SshHostKeyFingerprint is not set." 即使我在脚本中有指纹

Why am I getting "SessionOptions.SshHostKeyFingerprint is not set." even though I have the finger print in the script

我正在使用 SFTP 将文件传输到远程服务器,但我一直收到

SessionOptions.SshHostKeyFingerprint is not set.

我从 WinSCP 引用了这个文档 WinSCP .NET Assembly and COM Library

我做错了什么?请参阅下面的脚本:

try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "WinSCPnet.dll"

    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "psftp-nesam.ms.com"
        UserName = "test"
        Password = "MSR4sssssnd5"
        SshHostKeyFingerprint = "ssh-rsa 1024 +3t0xkOMWqfA7IaH0itjW/w7FQN+EtVyLDMTXk+D300=
    ssh-rsa 1024 23:6f:82:11:ec:b1:1a:82:95:1b:ed:7e:96:24:d0:11"
    }

    $session = New-Object WinSCP.Session

    try
    {
        # Connect
        $session.Open($sessionOptions)

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

        $transferResult =
            $session.PutFiles("E:\CMBPAID\BPAID0224_122700.csv", "/NESAMSCARIMED", $False, $transferOptions)

        # Throw on any error
        $transferResult.Check()

        # Print results
        foreach ($transfer in $transferResult.Transfers)
        {
            Write-Host "Upload of $($transfer.FileName) succeeded"
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }

    exit 0
}
catch
{
    Write-Host "Error: $($_.Exception.Message)"
    exit 1
}

使用你的代码我收到了不同的错误消息(但这可能是你的错误消息的根本原因):

The value supplied is not valid, or the property is read-only. Change the value, and then try again.

什么是正确的,因为 SshHostKeyFingerprint 值的语法是错误的。

如果您想指定多个替代指纹,请用分号分隔它们,而不是换行符:

SshHostKeyFingerprint = "ssh-rsa 1024 +3t0xkOMWqfA7IaH0itjW/w7FQN+EtVyLDMTXk+D300=;ssh-rsa 1024 23:6f:82:11:ec:b1:1a:82:95:1b:ed:7e:96:24:d0:11"

通过此更改,您的代码对我有用。

获得正确密钥的简单方法是 运行 WinSCP 应用程序,登录到您要访问的 SFTP 服务器,然后 select 从顶部的下拉菜单 "Sessions->Generate Session URL/Code...".这将显示一个漂亮的对话框,其中包含编写连接到此会话的代码的不同方法。 “.NET 程序集代码”选项卡包含建立相同连接所需的所有代码,包括 "SshHostKeyFingerprint".