Showing posts with label WinScp. Show all posts
Showing posts with label WinScp. Show all posts

Saturday, February 20, 2021

How to generate a code from WinSCP

Generate session URL/code dialog is used to generate a session URL, a file URL or a code template.

To open the dialog to generate the session URL or code, use:

  • Session > Generate Session URL/Code command on the main window to generate session URL or code template for the current session. (preferred)

  • Manage > Generate Session URL/Code command on the Login dialog to generate session URL or code template for the selected site.


Cheers!
Uma

Saturday, May 16, 2020

How to get SSH key in WinSCP

Once you connect to the SFTP server, go to session and click on Server/Protocol Information

Cheers!
Uma

Wednesday, May 2, 2018

How to download latest file from SFTP using PowerShell in SSIS

These days most of the organizations use SFTP files transfer protocol. There is still no any inbuilt component in SSIS to download the files from SFTP, but there is a component for FTP.

Most of use C# or PowerShell code to avoid cost for 3rd party components. In this blog, I explain how to use PowerShell with WinScp. First you need to install WinSCP library and most of the code you can get from https://winscp.net/forum

Here is the example code to download the latest file using wildcard


<#####################

Code

######################>

#Parameters

    $localPath = "D:\YourRemoteFolder\"

    $remotePath = "/YourFolder/"

$fileName="MyFiles"

#Define Wildcard

$fileWildcard = $fileName + "_" + ".zip"

# Load WinSCP .NET assembly 

    Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"

 # Setup session options

    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{

        Protocol = [WinSCP.Protocol]::Sftp

        HostName = "xx.xxx.xxx.xx"

        UserName = "user1"

        Password = "123456"

        SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"

}

Try

{

    $session = New-Object WinSCP.Session

        # Connect

        $session.Open($sessionOptions)

        # Get list of files in the directory

        $directoryInfo = $session.ListDirectory($remotePath)

        # Select the most recent file

        $latest =

            $directoryInfo.Files |

            Where-Object {$_.Name -like $fileWildcard } |

            Sort-Object LastWriteTime -Descending |

            Select-Object -First 1

         Write-host $latest

        $remotePathFull = $remotePath

                # check the file exists

            if ($latest -eq $Null)

            {

            Write-Host "No file found"

            exit 0

            }

            else 

            {

            # Download the selected file

            $remotePathFull = $remotePath+$latest


            #$session.GetFiles([WinSCP.RemotePath]::EscapeFileMask($latest.FullName), $localPath).Check()

            $session.GetFiles($remotePathFull, $localPath).Check()

            }   

}


finally

{

# Disconnect, clean up

$session.Dispose()

}

 exit 0

catch

{

    Write-Host "Error: $($_.Exception.Message)"

    exit 1

}

<#####################

Code End

######################>

You can call this PowerShell script using Execute Process Task under SSIS

Cheers!
Uma