SFTP PowerShell script with WinSCP

Advertisement

PlutoP
Guest

SFTP PowerShell script with WinSCP

Hey,

I'm trying to figure out how to take a file from a PC and upload it unto a SFTP server using WinSCP. I'm getting stuck after I run the command. First a credential window opens and I enter my credentials. That is successful! but then...I get a message that says...
cmdlet Move-WinSCPItem at command pipeline position 1
Supply values for the following parameters:
WinSCPSession:
I don't know what to put in for this WinSCPSession value. It just says...
Move-WinSCPItem : Cannot process argument transformation on parameter 'WinSCPSession'. Cannot convert the "" value of type "System.String" to type "WinSCP.Session".
At line:3 char:1
+ Move-WinSCPItem -Path "/PlutoP/NewFolder/Document.csv" -Destination "C:\U ...
I've tried Send-WindSCPItem, Move-WinSCPSessionOption, Receive-WinSCPItem, New-WinSCPSession, New-WinSCPSessionOption. Needless to say, I am new to this and would be grateful for any assistance!

Here's the script:
# Piping the WinSCPSession into the Receive-WinSCPItem auto disposes the WinSCP.Session object after completion.
New-WinSCPSessionOption -Hostname "SFTP server address" -Credential (Get-Credential) -SshHostKeyFingerprint "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
Move-WinSCPItem -Path "/PlutoP/NewFolder/Document.csv" -Destination "C:\Users\PlutoP\Download\Document.csv"

Reply with quote

Advertisement

PlutoP
Guest

Using WinSCP .NET Assembly from PowerShell

I also tried using WinSCP .NET Assembly from PowerShell
# Load WinSCP .NET assembly
Add-Type -Path "C:\Users\PlutoP\Downloads\NewFolder\WinSCPnet.dll"
 
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "SFTP server address"
    UserName = "PlutoP"
    Password = "xxxxxxxxxx"
    SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
}
 
$session = New-Object WinSCP.Session
 
try
{
    # Connect
    $session.Open($sessionOptions)
 
    # Transfer files
    $session.PutFiles("C:\Users\PlutoP\Download\Document.csv", "/PlutoP/NewFolder/*").Check()
}
finally
{
    $session.Dispose()
}
So this actually works, but I do not want username and password in this script.
I'd rather use Public/Private Keys but I need help with replacing the username/password code with some string: SshPrivateKeyPath and using the .pub and .ppk files to authenticate.

Thank you!
PlutoP

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,378
Location:
Prague, Czechia

Re: Using WinSCP .NET Assembly from PowerShell

PlutoP wrote:

I'd rather use Public/Private Keys but I need help with replacing the username/password code with some string: SshPrivateKeyPath and using the .pub and .ppk files to authenticate.
Correct, set SessionOptions.SshPrivateKeyPath to the path of your .ppk file.
https://winscp.net/eng/docs/library_sessionoptions#sshprivatekeypath

Easiest is to setup your session with private key in WinSCP GUI and have it generate a code template for you:
https://winscp.net/eng/docs/ui_generateurl#code

Reply with quote

PlutoP
Guest

Hey, Martin.

Thanks for the reply! So, I tried it with using the keys, but it seems like the server is refusing it for some reason so I'm going to have to look at the logs and troubleshoot this issue.
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 "PlutoP".
Server refused our key.
Authentication failed."
At C:\Users\PlutoP\Documents\Doc.ps1:20 char:5
+     $session.Open($sessionOptions)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SessionRemoteException

Reply with quote

Advertisement

Guest

I receive this message after I had added the public key to our server. It's a different message than the one I was getting before.
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 "PlutoP".
Authenticating with public key "rsa-key-20190501".
Authentication failed."
At C:\Users\PlutoP\Documents\powershellscript.ps1:20 char:5
+     $session.Open($sessionOptions)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SessionRemoteException

Reply with quote

PlutoP
Guest

SSH Key Passphrase

Hey, Martin.

I believe I know why it's failing now. It's looking for the SSH Key Passphrase.
I had set a passphrase for the key itself. Is there code I can enter in the script to enter passphrase or should I make a new key without one? I have attached the log file for more details.

Thank for your help!
PlutoP
  • SessionLog.Log (5.73 KB, Private file)

Reply with quote

PlutoP
Guest

Thanks Martin! That worked and everything else is going through with my script.
You're a freaking rockstar! I'll donate to you when I get home.

Reply with quote

Advertisement

You can post new topics in this forum