Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload file to SFTP using PowerShell

We were asked to set up an automated upload from one of our servers to an SFTP site. There will be a file that is exported from a database to a filer every Monday morning and they want the file to be uploaded to SFTP on Tuesday. The current authentication method we are using is username and password (I believe there was an option to have key file as well but username/password option was chosen).

The way I am envisioning this is to have a script sitting on a server that will be triggered by Windows Task scheduler to run at a specific time (Tuesday) that will grab the file in question upload it to the SFTP and then move it to a different location for backup purposes.

For example:

  • Local Directory: C:\FileDump

  • SFTP Directory: /Outbox/

  • Backup Directory: C:\Backup

I tried few things at this point WinSCP being one of them as well as SFTP PowerShell Snap-In but nothing has worked for me so far.

This will be running on Windows Server 2012R2.
When I run Get-Host my console host version is 4.0.

Thanks.

like image 756
Konstantin V Avatar asked Aug 02 '16 23:08

Konstantin V


People also ask

Is there a way to enable SFTP in PowerShell?

There isn't currently a built-in PowerShell method for doing the SFTP part. You'll have to use something like psftp.exe or a PowerShell module like Posh-SSH. Here is an example using Posh-SSH:

Can posh-SSH help with SFTP and SFTP?

I'm fairly new to SFTP and PowerShell as well, and I need help downloading files from an SFTP server onto my application server. I believe that the Posh-SSH module can help with establishing an SFTP connection, but most code I have seen until now involves uploading files onto the SFTP server, not downloading from... would appreciate any advice!

How to upload a binary file to FTP server using PowerShell?

The input here must have USER [USERNAME] [PASS] on a single line after the OPEN [HOSTNAME], per the previous comment. The most trivial way to upload a binary file to an FTP server using PowerShell is using WebClient.UploadFile:

How do I download files from another server using SFTP?

To download files, you must first set up a session. The session is a persistent connection you’ll use to perform various SFTP operations. To set up a session, provide a PSCredential object via Get-Credential and specify the remote SFTP server you’d like to connect to using the New-SFTPSession command.


1 Answers

You didn't tell us what particular problem do you have with the WinSCP, so I can really only repeat what's in WinSCP documentation.

  • Download WinSCP .NET assembly.
    The latest package as of now is WinSCP-5.19.6-Automation.zip;

  • Extract the .zip archive along your script;

  • Use a code like this (based on the official PowerShell upload example):

    # Load WinSCP .NET assembly Add-Type -Path "WinSCPnet.dll"  # Setup session options $sessionOptions = New-Object WinSCP.SessionOptions -Property @{     Protocol = [WinSCP.Protocol]::Sftp     HostName = "example.com"     UserName = "user"     Password = "mypassword"     SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx...=" }  $session = New-Object WinSCP.Session  try {     # Connect     $session.Open($sessionOptions)      # Upload     $session.PutFiles("C:\FileDump\export.txt", "/Outbox/").Check() } finally {     # Disconnect, clean up     $session.Dispose() } 

You can have WinSCP generate the PowerShell script for the upload for you:

  • Login to your server with WinSCP GUI;
  • Navigate to the target directory in the remote file panel;
  • Select the file for upload in the local file panel;
  • Invoke the Upload command;
  • On the Transfer options dialog, go to Transfer Settings > Generate Code;
  • On the Generate transfer code dialog, select the .NET assembly code tab;
  • Choose PowerShell language.

You will get a code like above with all session and transfer settings filled in.

Generate transfer code dialog

(I'm the author of WinSCP)

like image 167
Martin Prikryl Avatar answered Sep 24 '22 21:09

Martin Prikryl