Get-SFTPFiles function for PowerShell

  |   Martin Vobr

This is a guest post by Tim Smiths, user of Rebex SFTP, who was so kind to share his real-world experience with using SFTP from PowerShell script.

Suppose that we have a few imports that require us getting the latest files from an SFTP server, and we're looking to automate the process using PowerShell. In addition, we want it built in a way that we can use on other imports, or on possible future imports. With Rebex, this task becomes easy and PowerShell makes the task even easier. I'll assume that most readers know a little about PowerShell and calling functions; if you don't, for the sake of simplicity, keep the function saved to a .ps1 file and call the file when using it.

Let's look at an example of using the SFTP Rebex and how we can access the server by passing in a few credentials, getting the latest file(s), and save the file(s) to a local location. In the below code, I use the Add-Type function, which is a non deprecated feature in PowerShell, in addition to it being much easier and more intuitive, and pass in the location of the Rebex SFTP dll. Next, our function will take in certain parameters that, more than likely, will change when moving from one SFTP server to the next when obtaining files.

Parameters:

  1. $sftp _srv: the SFTP server location, for instance ftp.ourserver.com.
  2. $user: the username to the SFTP server.
  3. $pass: the password to the SFTP server.
  4. $dest: the destination for the files we get (ie: download) from the SFTP server.
  5. $days: the days within the last week to get new files (note the validation range between 0 and 7 - if a longer or shorter time frame is necessary, we can change this); 0 if we want to get all of the files.
Function Get-SFTPFiles {  
    Param (
        [Parameter(Mandatory=$true)]
        [string]
        $sftp_srv
        ,
        [Parameter(Mandatory=$true)]
        [string]
        $user
        ,
        [Parameter(Mandatory=$true)]
        [string]
        $pass
        ,
        [Parameter(Mandatory=$true)]
        [string]
        $dest
        ,
        [Parameter(Mandatory=$true)]
        [ValidateRange(-7,0)]
        [Int]
        $days
    )
    Process
    {
        Add-Type -Path "C:\Program Files (x86)\Rebex File Transfer Pack for .NET 4.0 \SFTP for .NET 4.0\bin\Rebex.Net.Sftp.dll"
        $sftp = New-Object Rebex.Net.Sftp
        $sftp.Connect($sftp_srv)
        $sftp.Login($user,$pass)  
        if ($days -eq 0)
        {
            $items = $sftp.GetList()
        }
        else
        {
            $dt = (Get-Date).AddDays($days)
            $items = $sftp.GetList() | Where-Object {$_.Created -ge $dt}
        }
        foreach ($item in $items)
        {
            $grab = $item.Name
            $save = $dest + $grab
            $sftp.GetFile($grab, $save)
        }
        $sftp.Disconnect()
        $sftp.Dispose()
    } }

That's our function, and how we'd call it (using a test example):

Get-SFTPFiles -sftp_srv "srvdev.root.oursite.com" -user "OurUserName" -pass "OurPassword?8%" -dest "C: SFTP Saved " -days 0  

See how easy that makes it? Now, we can re-use this code for our other SFTP servers where we get new files within a time range. Combine the above script with a schedule, and we never have to worry about missing a file or checking the SFTP server to see what's there. The best part is that we can re-use this for our other SFTP imports and save time from worrying about new development efforts on SFTP tasks.

Tim Smith works as a DBA Developer for RS Consulting Partners, as well as teaches the course Automating ETL and Big Data Development on Udemy.