WinSCP正在从xml文件中获取密码



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

以下是有效的脚本:

[xml]$Config = Get-Content "*Config.xml"
# Load WinSCP .NET assembly
Add-Type -Path "C:Program Files (x86)WinSCPWinSCPnet.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.Cnfiguration.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

如有任何帮助,我们将不胜感激!谢谢

解决方案:

用户名=$config.config.UserName

安全密码=转换为SecureString$config.Configuration.Password

我将尝试在不提供太多密码细节的情况下提供主要想法。

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

所以当";实际密码";(不是TRUE密码(被输入到xml文件中,它被正确地读取到PowerShell中,但密码不正确。所以我花了一段时间才发现xml文件没有确切的TRUE密码。

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

最新更新