Topic "[SOLVED] - Ignore password promt when useing Private/Publik Keys?"

Author Message
Muffinz
[View user's profile]

Joined: 2016-12-15
Posts: 7
Location: Sweden
Hi,

I'm trying to get my Powershell script to login and upload som files on a FTP server. It works great with Putty/psftp.exe.

The connection is SFTP with a username, a Key-file that has a passphrase on it and no password.
But I can't get it to work with WinSCP .Net or the Powershell module.

This gets me a login promt at $session:
Code:

$sshKeyFinger = "ssh-rsa 2048 xx:cc:vv:bb"
$sshKeyPath = "\\UNC\Path\to\Key.ppk"
$sshpw =  ConvertTo-SecureString "Password" -AsPlainText -Force

$session = New-WinSCPSession -HostName Username@host.com -SshPrivateKeyPath $sshKeyPath  -SshPrivateKeySecurePassphrase $sshpw -SshHostKeyFingerprint $sshKeyFinger

Send-WinSCPItem -Path \\local\unc\path\* -WinSCPSession $session -Destination /transfer

Remove-WinSCPSession -WinSCPSession $session



and with this:
Code:

# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"

# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "Host"
    UserName = "UserName"
    Password = "SSHKey_Passphrase"
    SshHostKeyFingerprint = "ssh-rsa 2048 xx:cc:vv:bb"
    SshPrivateKeyPath = "\\UNC\Path\to\Key.ppk"
}

$sessionOptions.AddRawSettings("AuthKI", "0")
$session = New-Object WinSCP.Session

try
{
    # Connect
    $session.Open($sessionOptions)
   
    # Your code
    $transferOptions = New-Object WinSCP.TransferOptions
    $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
 
    $transferResult = $session.PutFiles("\\Local\UNC\Path\*", "/transfer", $False, $transferOptions)

    # Throw on any error
    $transferResult.Check()

        # Print results
        foreach ($transfer in $transferResult.Transfers)
        {
            Write-Host ("Upload of {0} succeeded" -f $transfer.FileName)
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }


I get this error message:
Code:

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 "Username".
Authenticating with public key "rsa-key-".
Authentication failed."
At line:20 char:5
+     $session.Open($sessionOptions)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SessionRemoteException



Any idea?

Last edited by Muffinz on 2016-12-27 10:47; edited 1 time in total
Advertisements
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25966
Location: Prague, Czechia
Please attach a full session log file showing the problem (using the latest version of WinSCP).

To generate the session log file, set Session.SessionLogPath. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you can mark the attachment as private.
Guest




martin wrote:
Please attach a full session log file showing the problem (using the latest version of WinSCP).

To generate the session log file, set Session.SessionLogPath. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you can mark the attachment as private.


Hi,

I "solved" it with this code Smile

Code:

# Load WinSCP .NET assembly
Add-Type -Path "\\srv\Script\FTP\WinSCP-5.9.3-Automation\WinSCPnet.dll"

# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "HostName"
    UserName = "UserName"
    SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
    SshPrivateKeyPath = "\srv\Script\FTP\psftp\Key.ppk"
}



$session = New-Object WinSCP.Session
$session.SessionLogPath = "C:\Temp\Log\Uploaded.log"
$sessionOptions.SshPrivateKeyPassphrase = "Password"
try
{
    # Connect
    $session.Open($sessionOptions)

    # Your code

    $localPath = "C:\Temp\test\*"
    $remotePath = "/SomeRemoteFolder"

    # Upload files, collect results
    $transferResult = $session.PutFiles($localPath, $remotePath)

    Write-Host ("Upload of {0} succeeded" -f
    $transfer.FileName)
           
}
finally
{
# Disconnect, clean up
$session.Dispose()
}

 
catch [Exception]
{
Write-Host $_.Exception.Message
exit 1
}


But now I get another error code instead:

Code:

Error code: 4
Error message from server: Failure
 
Common reasons for the Error code 4 are:
Renaming a file to a name of already existing file.
Creating a directory that already exists.
Moving a remote file to a different filesystem (HDD).
Uploading a file to a full filesystem (HDD).
Exceeding a user disk quota.
Asking user:
Cannot overwrite remote file '/SomeRemoteFolder'.$$
Uploaded.log (12.62 KB) Private file

Description: The Log file

martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25966
Location: Prague, Czechia
I assume the problem is that you have not terminated the target path with a slash.

Try this:

$remotePath = "/SomeRemoteFolder/"

See https://winscp.net/eng/docs/library_session_putfiles#remotepath
Muffinz
[View user's profile]

Joined: 2016-12-15
Posts: 7
Location: Sweden
martin wrote:
I assume the problem is that you have not terminated the target path with a slash.

Try this:

$remotePath = "/SomeRemoteFolder/"

See https://winscp.net/eng/docs/library_session_putfiles#remotepath


Ah! I missed that, Thank you!
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License