1

Is it possible to securely routinely poll an external FTPS (or SFTP) server and copy/move the files locally using standard Windows Server 2012 functionality?

I imagine the use of scheduler to do the polling/movement, but is there capability to add FTPS/SFTP into the mix?

The only guidance I’ve come across is turning the server into an FTPS server, but not how to utilize the OS’s FTPS client…if there is one.

Thanks

TessellatingHeckler
  • 5,726
  • 3
  • 26
  • 44
BlueFrog
  • 13
  • 1
  • 3
  • 1
    Done such a thing, powershell using winscp's dotnet assembly to acces the sftp server. Start with winscp, look for how to register that assembly, then how to acces it from powershell, then examples of ftp polling. I suppose you can also use other languages if you're not into powershell. – Dan May 27 '15 at 18:02
  • I believe there are even 100% Powershell native implementations of SSH/SFTP out there as well that use nothing but the .NET class libraries available in the base OS. But you're still never going to write something like that yourself. And at the point you're downloading someone else's script, there's not much difference between downloading that vs a binary utility like putty or winscp. – Ryan Bolger May 28 '15 at 05:56

2 Answers2

2

There's no FTPS nor SFTP client in Windows (on any version).

The built-in Windows ftp.exe supports a plain unencrypted FTP only. Moreover it supports the active FTP mode only, what makes it pretty useless, when connecting to a server behind a firewall or NAT.


You need to use a 3rd party client.

For example with WinSCP FTP/SFTP client, you can poll an FTPS server with a batch file like:

winscp.com /command ^
    "open ftps://user:password@example.com/" ^
    "get /path/file c:\path\" ^
    "exit"

Similarly for the SFTP:

winscp.com /command ^
    "open sftp://user:password@example.com/ -hostkey=""ssh-rsa 2048 xxxxxxxxxxx...=""" ^
    ...

See the guide to scripting with WinSCP.

And then just schedule the script with Windows Scheduler.

(I'm the author of WinSCP)


You can also use the FtpWebRequest class from .NET framework from a PowerShell script. It supports FTPS (but not SFTP). Though Microsoft does not recommend it for a new development. Anyway, for an example, see the answer by @TessellatingHeckler.

Martin Prikryl
  • 7,756
  • 2
  • 39
  • 73
  • 1
    Thanks for the quick response. I was checking out WinSCP as a plan B - sounds like it'll do the job perfect. – BlueFrog May 28 '15 at 08:35
1

It looks like you can do it with standard Windows tools, using the .Net Framework's FTPWebRequest class from PowerShell, with the EnableSsl property to enable FTPS.

There's an example here: https://stackoverflow.com/q/1279041/478656 which includes the code (I haven't tested):

# Create an FTPWebRequest object to handle the connection to the FTP server
$ftprequest = [System.Net.FtpWebRequest]::Create($sourceuri)

# Set the request's network credentials for an authenticated connection
$ftprequest.Credentials = New-Object System.Net.NetworkCredential($username,$password)

# Set FTPWebRequest method to ListDirectory
$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectory
$ftprequest.EnableSsl = $True
$ftprequest.UseBinary = $False
$ftprequest.UsePassive = $True
$ftprequest.KeepAlive = $False

$ftpresponse = $ftprequest.GetResponse()

Write-Out $ftpresponse.StatusCode
Write-Out $ftpresponse.StatusDescription

I'd look at changing UseBinary to $true and then using the DownloadFile() method.

And then calling the script from Task Scheduler as you suggested.

TessellatingHeckler
  • 5,726
  • 3
  • 26
  • 44