Please don't lecture me, I wrote my first computer program almost 40 years ago in CS in undergrad college. PS is just another tool, not God's child, and I am new PS usder) I connect and upload files OK to the external SFTP site with WinSCP GUI. SFTP server does not use a password but authenticates with SSH key exchange, so I have no password parameter when using WinSCP in PowerShell script (below) error says:
Connection has been unexpectedly closed. Server sent command exit status 0.
Authentication log (see session log for details):
Using username "myusername".
Authentication failed.
Question 1) Whose log and where is it? Do I need to $it
Question 2) Any big errors in PS scrript?
here is PS code: (I call without parameters for now as it is single use at this time)
$SHA256 = 'ssh-ed25519 256 blabal.....................'
$MD5 = 'ssh-ed25519 256 c8:bf5:12:c:28:2d:16............'
$username = 'myusername'
$site = 'ftp.ftpsite.com'
function Send-Sftp {
try
{
# Setup Session Options
$sessionlog = "C:\mendo\software\conexed_package\winscplog.txt"
$SshPrivateKeyPath = "C:\mendo\software\conexed_package\PPKeys\Mendo-ConexED_private_key.ppk"
$skey = C:\mendo\software\conexed_package\PPKeys\Mendo-ConexED_private_key.ppk
$SessionOptions = New-Object WinSCP.SessionOptions
$SessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$SessionOptions.HostName = "$site"
$SessionOptions.UserName = "$Username"
$SessionOptions.PassWord = "$pass"
$SessionOptions.sshHostKeyFingerprint = "$SHA256"
#$sessionOptions.executablepath = "C:\Program Files\WindowsPowerShell\Modules\winscp\5.13.8.0\bin"
$source = "c:\mendo\software\conexed_package\mendocino.csv"
$dest = "/import/*.*"
'In send-sftp; before opening session'
$Session = New-Object WinSCP.Session
try
{
$session.Connect
$Session.open($SessionOptions)
# upload files -------------------
'In send-sftp; after opening session'
$TransferOptions = New-Object WinSCP.TransferOptions
$TransferOptions.TransferMode = [WinsSCP.TransferMode]::Binary
$TransferResult = $Session.PutFiles("$Source", "$Dest". "False",
#is put file ok or do I need send>
$TransferOptions)
# Print Results -------------------
foreach ($Transfer in $TransferResult.Transfers)
{
Write-host -foregroundcolor green("Upload of {0} succeded" -f $Transfer.Filename)
}
}
finally
{
# disconnect and clean up
$Session.Dispose()
}
#exit 0
}
catch [exception]
{
write-host $_.Exception.message
exit 1
}
}