WinSCP 从 xml 文件获取密码

问题描述 投票:0回答:2

我已成功创建使用 WinSCPnet.dll(.NET 程序集)的语法。 此外,还创建了一个 config.xml 文件以提供凭据并启动会话。 我用除了密码以外的所有东西都成功地做到了这一点!

这是有效的脚本:

[xml]$Config = Get-Content "*\Config.xml"
# Load WinSCP .NET assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = $Config.Configuration.HostIP
    UserName = $Config.Configuration.UserName
    Password = "Actual Password"
    PrivateKeyPassphrase = $Config.Configuration.PassPhrase
    SshHostKeyFingerprint = $Config.Configuration.FingerPrint
    SshPrivateKeyPath = $Config.Configuration.SshPrivateKeyPath
}

我无法通过用 $Config.Configuration.Password 替换“实际密码”来让它工作。当我自己运行它时,我可以获得对象 $Config.Configuration.Password 以输出正确的密码。在 xml 文件中,我考虑了转义字符。有谁知道这不起作用的其他原因?

错误信息:

Exception calling "Open" with "1" argument(s): "Connection has been unexpectedly closed. Server sent command exit status 0.
Authentication log (see session log for details):
Using username "Actual Username".
Authenticating with public key "imported-openssh-key" from agent.
Further authentication required
Access denied.
Authentication failed."
At *\PS_winSCP.ps1:25 char:5
+     $session.Open($sessionOptions)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SessionRemoteException

任何帮助将不胜感激! 谢谢

powershell sftp winscp winscp-net
2个回答
0
投票

我将尝试提供主要思想而不提供太多有关密码的详细信息。

“实际密码”是我直接输入 PowerShell 以打开会话的密码。这不一定与服务器的 TRUE 密码匹配。 PowerShell 不能使用 TRUE 密码,因为它有一些自己的特殊字符。

因此,当“实际密码”(不是真正的密码)输入 xml 文件时,它被正确读入 PowerShell,但却是不正确的密码。所以我花了一段时间才弄清楚 xml 文件没有确切的 TRUE 密码。

长话短说;尝试确保 xml 文件中的密码完全正确。


0
投票

解决方案:

用户名 = $config.configuration.UserName

SecurePassword = ConvertTo-SecureString $config.Configuration.Password

© www.soinside.com 2019 - 2024. All rights reserved.