This script is an example from here: https://winscp.net/eng/docs/library_powershell
I have just edited paths and session options.
I can successfully connect with to the sftp server with the private key in WinSCP GUI.
try
{
# Load WinSCP .NET assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "test.sftp.com"
UserName = "customer_TEST"
# I do not want to use this: Password = "mypassword"
SshHostKeyFingerprint = "ssh-rsa 2048 SHA256:*********"
}
$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("D:\test\*", "/home/user/", $False, $transferOptions)
# Throw on any error
$transferResult.Check()
# Print results
foreach ($transfer in $transferResult.Transfers)
{
Write-Host "Upload of $($transfer.FileName) succeeded"
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch
{
Write-Host "Error: $($_.Exception.Message)"
exit 1
}
CodePudding user response:
SSH host key fingerprint is not an authentication method. It's a way to verify an identity of the server, not yours. See Understanding SSH key pairs.
If you want to use public key authentication, you need to set SessionOptions.SshPrivateKeyPath
(or SessionOptions.SshPrivateKey
).
And you will have to set SshHostKeyFingerprint
correctly, as your current value will throw an error:
WinSCP .NET assembly in PowerShell - Creating SessionOptions - The value supplied is not valid, or the property is read-only