Polling FTPS/SFTP Server with Windows Server 2012

8,453

Solution 1

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:[email protected]/" ^
    "get /path/file c:\path\" ^
    "exit"

Similarly for the SFTP:

winscp.com /command ^
    "open sftp://user:[email protected]/ -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.

Solution 2

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.

Share:
8,453

Related videos on Youtube

BlueFrog
Author by

BlueFrog

Updated on September 18, 2022

Comments

  • BlueFrog
    BlueFrog over 1 year

    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

    • Dan
      Dan about 9 years
      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.
    • Ryan Bolger
      Ryan Bolger about 9 years
      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.
  • BlueFrog
    BlueFrog about 9 years
    Thanks for the quick response. I was checking out WinSCP as a plan B - sounds like it'll do the job perfect.