Transfer File SFTP

Jan Kowalski 1 Reputation point
2022-08-16T20:39:35.537+00:00

Hi Everybody

I have a one problem with my small etl process, im trying to create a process that copy all files from SFTP server to my localdisk. SFTP server have login, password, i dont wanna move all files from SFTP but copy all files from SFTP. Do you know any examples code that solve this problem ? I'm creating code but he's not working.

I see a lot of articles on this problem, but this articles describing how to move file. Files on the SFTP server must preserve.

Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
10,931 questions
Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,403 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,427 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Dillon Silzer 54,926 Reputation points
    2022-08-17T02:44:31.613+00:00

    Hi @Jan Kowalski

    You can use PowerShell and POSH-SSH to complete such a task (Most of script borrowed from https://community.spiceworks.com/topic/2233481-posh-ssh-downloading-from-an-sftp-server (made edits to it so do not use this one or you will potentially delete all your files on the server). Additionally, the script on this is outdated and some commands needed to be updated.)

    1) Run PowerShell or PowerShell ISE as Administrator

    2) Run the command Set-ExecutionPolicy -ExecutionPolicy Unrestricted (make sure to restrict after you are done using the script).

    3) Run the command install-module posh-ssh

    4) Run the command import-module posh-ssh

    5) Create an encrypted key for $encrypted

    a) Type $secure = Read-Host -AsSecureString and a window will appear in PowerShell

    Type in your password in the window and hit OK

    231770-image.png

    b) Run the command $encrypted = ConvertFrom-SecureString -SecureString $secure

    c) Run the command $encrypted and your string will appear.

    231756-image.png

    d) Copy and paste the string into $encrypted (in the script below) and ensure that it is on one line.

    The following example for $encrypted will work:

    231807-image.png

    The following example for $encrypted will not work:

    231757-image.png

    e) Fill in $ComputerName with your server's IP address.

    Fill in $username with the username on the sftp server.

    Change $LocalPath to a folder on your device.

    Change $SftpPath to the directory location on the sftp server.

    f) Run the script below with all the steps above completed and enjoy downloading all of your files from a directory.

    # Define Server Name  
    $ComputerName = "127.0.0.1"  
      
    # Define UserName  
    $username = "username"  
      
    $encrypted = "InsertEncryptedStringHere"  
      
    $password = ConvertTo-SecureString -String $encrypted  
      
    #Set Credetials to connect to server  
    $Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $username, $password  
      
    # Set local file path and SFTP path  
    $LocalPath = "C:\Users\username\Downloads\test\"  
    $SftpPath = '/var/www/html/'  
      
    # Establish the SFTP connection  
    $SFTPSession = New-SFTPSession -ComputerName $ComputerName -Credential $Credential -AcceptKey  
      
    # lists directory files into variable  
    $FilePath = Get-SFTPChildItem -sessionID $SFTPSession.SessionID -path $SftpPath  
      
    #For each file listed in the directory below copies the files to the local directory and then deletes them from the SFTP one at a time looped until all files  
    #have been copied and deleted  
    ForEach ($LocalFile in $FilePath)  
    {  
          
    #Ignores '.' (current directory) and '..' (parent directory) to only look at files within the current directory  
        if($LocalFile.name -eq "." -Or $LocalFile.name -eq ".." )  
        {  
              Write-Host "Files Ignored!"  
        }  
        else  
        {  
            Write-Host $LocalFile  
            get-sftpitem -SessionId $SFTPSession.SessionID -Path $localfile.fullname -Destination $LocalPath -Force  
      
        }     
      
    }  
      
    #Terminates the SFTP session on the server  
    Remove-SFTPSession -SessionId $SFTPSession.SessionID  
    

    Example

    What's on my server (via WinSCP):

    231740-image.png

    What was downloaded with the script (locally):

    231796-image.png

    Get-SFTPItem (Documentation)

    https://github.com/darkoperator/Posh-SSH/blob/master/docs/Get-SFTPItem.md

    -------------------------------------------

    If this is helpful please accept answer.