简体   繁体   English

PowerShell 中的 WinSCP .NET 程序集 - 创建 SessionOptions - 提供的值无效,或者属性为只读

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

I'm trying to automate the transfer of files with WinSCP using PowerShell. I've installed .NET assembly.我正在尝试使用 PowerShell 通过 WinSCP 自动传输文件。我已经安装了 .NET 程序集。 In the code I've added, I've taken out credentials and hostname for privacy and I'm also using the full path to WinSC.net.dll .在我添加的代码中,出于隐私考虑,我删除了凭据和主机名,并且我还使用了WinSC.net.dll的完整路径。 Here is the error I'm getting:这是我得到的错误:

Error: The value supplied is not valid, or the property is read-only.错误:提供的值无效,或者该属性是只读的。 Change the value, and then try again.更改值,然后重试。

I'm getting this where $sessionOptions is defined on 5th line.我在第 5 行定义$sessionOptions的地方得到这个。 Any ideas of what I could do to get this running?关于我可以做些什么来让它运行的任何想法?

try
{
    Add-Type -Path "WinSCPnet.dll"

    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "HostName"
        UserName = "UserName"
        Password = "Password"
        SshHostKeyFingerprint = "ssh-rsa 2048 Fingerprint here"
    }

    $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("Source", "Destination", $False, $transferOptions)

        # Throw on any error
        $transferResult.Check()

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

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

The most probable explanation is that the value of SessionOptions.SshHostKeyFingerprint is invalid, no other property in your code is validated. 最可能的解释是SessionOptions.SshHostKeyFingerprint的值无效,代码中没有其他属性被验证。

Use WinSCP GUI function to generate a code template to get the correct value. 使用WinSCP GUI函数生成代码模板以获取正确的值。

Read also about Automatic host key verification . 另请阅读自动主机密钥验证


If you need more help, you will have to show us the real value you are using (it's a fingerprint of a server public key, hence it is a public information, no need to hide it). 如果您需要更多帮助,您必须向我们展示您正在使用的真正价值(它是服务器公钥的指纹,因此它是公共信息,无需隐藏它)。

I've run into this recently and the issue turned out to be how the host key is shown in the connection profile.我最近遇到了这个问题,结果证明是主机密钥在连接配置文件中的显示方式。 It was stripping off the trailing "=" character它正在剥离尾随的“=”字符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM