Hello guys, I need to write a code using PowerShell to connect to SFTP using private key. When I use the GUI, always ask for the passphrase, and that's ok for me, but when try to make a code for this and do not ask for the passphrase, it becomes a madness, please, help me, thanks.
Gus.
Part of the code in PS
# 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 = "xxxx.xxxx.xxxx"
UserName = "xxxxxx"
Password = "xxxxxx"
SshHostKeyFingerprint = "ssh-rsa 2048 df769da876gsf798g7gd"
SshPrivateKeyPath = "D:\Bops-SFTP\clave_privada.ppk"
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Your code
}
finally
{
$session.Dispose()
}
When make the connection I get this error
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 "xxxxxx".
Authenticating with public key "xxxxxxx".
Authentication failed."
At line:18 char:5
+ $session.Open($sessionOptions)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SessionRemoteException