Powershell Module for transferring files via SFTP

22. June 2013 23:50 by Jay Grossman in   //  Tags: , , ,   //   Comments (0)

I had the need for several automated jobs to be able to exchange files securely with partners via the Secure FTP (SFTP). I chose powershell because the jobs:

  1. get executed on windows nodes. It is less overhead than creating console apps in .NET
  2. required things like querying databases, transforming data, and archiving files

I spent a little bit of time looking into different options, and decided to utilize the latest version of the WinSCP library provided the required options - such as designating the transfer mode, deleting source files, and specifying file masks.

I created a generic powershell module that exposes upload and download functions. The module is available on github:

https://github.com/jaygrossman/SftpPowershellModule

Function signatures:

Function SFTPDownloadFiles {

      Param(

            [string$Username# Required. Username of SFTP account
            [string$PasswordRequired. Password of SFTP account
            [string$HostNameRequired. HostName of SFTP server
            [string$RemotePathRequired. Starting path of SFTP server
            [string$LocalPathRequired. Starting path of local server
            [string$SshHostKeyFingerprintRequired. Host Key, 
ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx
            [string$FileMask# Optional. Filter string to limit files to download
            [bool]   $Remove=$false# Optional. Whether to remove original file
            [string$TransferMode="binary"# Optional. Transfer can be binary, ascii, or automatic
      ) 

 

 Function SFTPUploadFiles {
      Param(
            [string$UsernameRequired. Username of SFTP account
            [string$PasswordRequired. Password of SFTP account
            [string$HostNameRequired. HostName of SFTP server
            [string$RemotePathRequired. Starting path of SFTP server
            [string$LocalPathRequired. Starting path of local server
            [string$SshHostKeyFingerprintRequired. Host Key, 
ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx
            [string$FileMask# Optional. Filter string to limit files to upload
            [bool]   $Remove=$false# Optional. Whether to remove original file
            [string$TransferMode="binary"Optional. Transfer can be binary, ascii, or automatic

Comments 

About the author

Jay Grossman

techie / entrepreneur that enjoys:
 1) my kids + awesome wife
 2) building software projects/products
 3) digging for gold in data
 4) rooting for my Boston sports teams:New England PatriotsBoston PatriotsBoston Red SoxBoston CelticsBoston Bruins

Month List