In a previous post I went over how to use WinSCP and Powershell to send files to an FTP server. In this post I will go over receiving files via WinSCP and Powershell. If you haven’t yet take a look at this post as it goes over a few things that I will not touch in this post.
WinSCP as some examples of this on there site, but to be honest I think they over complicated it, heres the solution I am currently using:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
function Receive-FTP([string]$user, [string]$pass, [string]$site, [string]$key, [string]$source, [string]$dest){ try { # Setup session options $sessionOptions = New-Object WinSCP.SessionOptions $sessionOptions.Protocol = [WinSCP.Protocol]::Sftp $sessionOptions.HostName = "$site" $sessionOptions.UserName = "$user" $sessionOptions.Password = "$pass" $sessionOptions.SshHostKeyFingerprint = "$key" $session = New-Object WinSCP.Session try { # Connect $session.Open($sessionOptions) #List Dir $listDir = $session.ListDirectory($ftpSource) foreach ($file in $listDir.Files) { $transferResult = $session.GetFiles($ftpSource, $ftpDest).Check() } # Print results foreach ($transfer in $transferResult.Transfers) { Write-Host -foregroundcolor green ("Upload of {0} succeeded" -f $transfer.FileName) } } finally { # Disconnect, clean up $session.Dispose() } #exit 0 } catch [Exception] { Write-Host $_.Exception.Message exit 1 } } |
We are pretty much doing the exact same thing as shown in my sending files post, but now we are using the getfiles method. Hope that helps!