WinSCP Power shell 脚本在连接到 SFTP 服务器时生成错误 "The value supplied is not valid, or the property is read-only"

WinSCP Power shell script is generating error "The value supplied is not valid, or the property is read-only" while connecting to SFTP server

我正在尝试从我们的 Windows 服务器连接到客户端 SFTP 服务器以提取文件。当使用 WinSCP 应用程序和 PowerShell 脚本从我的本地计算机尝试时,连接工作正常。但是我在尝试从服务器连接时必须添加代理设置,而在本地连接中则不是这样。

在服务器中,我可以通过启用代理设置从 WinSCP 应用程序进行连接,但是当我尝试使用 PowerShell 脚本进行连接时却失败了。我从 WinSCP 连接获得了自定义生成的 PowerShell 脚本。 下面是代码片段和相应的错误消息。

$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::SFTP
    HostName = "<host name>"
    PortNumber = 22
    UserName = "<Username>"
    SshPrivateKeyPath= "<private key>"  
    SshHostKeyFingerprint = "<SshHostKeyFingerprint>"
}

$sessionOptions.AddRawSettings("ProxyMethod", "3")
$sessionOptions.AddRawSettings("ProxyHost", "<proxyhost>")
$sessionOptions.AddRawSettings("ProxyPort", "3128")
$sessionOptions.AddRawSettings("ProxyLocalhost", "1")

收到以下错误消息。

New-Object : The value supplied is not valid, or the property is read-only. Change the value, and then try again.
At line:3 char:19
+ $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
+                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [New-Object], Exception
    + FullyQualifiedErrorId : SetValueException,Microsoft.PowerShell.Commands.NewObjectCommand
 
You cannot call a method on a null-valued expression.
At line:12 char:1
+ $sessionOptions.AddRawSettings("ProxyMethod", "3")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
You cannot call a method on a null-valued expression.
At line:13 char:1
+ $sessionOptions.AddRawSettings("ProxyHost", "<proxyhost>")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
You cannot call a method on a null-valued expression.
At line:14 char:1
+ $sessionOptions.AddRawSettings("ProxyPort", "3128")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
You cannot call a method on a null-valued expression.
At line:15 char:1
+ $sessionOptions.AddRawSettings("ProxyLocalhost", "1")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

最可能的解释是 SessionOptions.SshHostKeyFingerprint 的值无效,您代码中的其他 属性 未通过验证。

另见 WinSCP .NET assembly in PowerShell - Creating SessionOptions - The value supplied is not valid, or the property is read-only

由于您的指纹格式看起来是正确的,但由于它是“现代”ssh-ed25519 密钥,我假设您有不接受此密钥的旧版本 WinSCP .NET 程序集。您可能使用较新版本的 WinSCP(GUI)来获取此密钥,但您的 .NET 程序集版本不接受它。确保使用最新版本的程序集。