SFTP from scheduled tasks

I was asked to set up a daily SFTP job to push files off a Windows box.

Windows has FTP built-in. But SFTP? Well there’s the free (and open-source) Putty implementation which provides a number of SSH tools, including SFTP. Free is good. I like free.

Putty SFTP is fairly straightforward to use from the command line; pretty similar to any other ftp client. Basically chuck everything into a directory and you can run it directly. Under Windows it caches public keys into the Windows Registry in HKEY_CURRENT_USER, and will ask about this the first time.

For batch use, you can create a BAT file that calls it like this

psftp -l [user] -pw [password] [host] -P [port] -b [scriptfile]

…and just put your FTP commands into the script file.

For running it as a scheduled task, you’d preferably want to run it under a special user, as you wouldn’t want it under your own user account. It seems not to like asking about caching unless you’re the person logged in. Even running with RUNAS didn’t work for me — it flashed up something lightning-fast, but immediately failed.

The only way around it I could find was to log on interactively as the scheduled task user. As expected it asked the first time. Subsequent times it didn’t ask, and worked happily as a scheduled task.

(Also found recently: How to set up an SFTP server on a Windows machine. Or a prefab one like FreeFtpD is also an option.)