﻿
$remotepath = "/out/"
$date = get-date -format dd.MM.yyyy
$localPath= "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\images downloaded on $date"
mkdir "$localpath"

# Load WinSCP .NET assembly
Add-Type -Path "C:\WinSCP_PS\WinSCPnet.dll"
 
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "XXXXXXXXXXXXXXX"
    UserName = "XXXXXXXXX"
    Password = "XXXXXXXXXXXXXXX"
    SshHostKeyFingerprint = "XXXXXXXXXXXXXXXXXXXXXXXXX"
    
    


}
$logFilePath = "C:\WinSCP_FTP_logs\XXXXXXXXXX_FTP_$date.log"
$downloadedFilesFile = "C:\WinSCP_FTP_logs\FTP_downloaded_files.txt"
$session = New-Object WinSCP.Session

 
try
{
    #log
    $session.SessionLogPath = $logFilePath
 
    # Connect
    $session.Open($sessionOptions)

    
    # Transfer files
    $transferOptions = New-Object WinSCP.TransferOptions
    $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
    $transferResult = $session.GetFiles($remotepath, $localPath + "\*", $false, $transferOptions)
    if ($transferResult.IsSuccess) {
    # Write the file names to the downloaded files file
    foreach ($transfer in $transferResult.Transfers) {
        $transfer.FileName | Out-File $downloadedFilesFile -Append
    }
    }

}
finally
{
    $session.Dispose()
}