Automate SFTP Integration Tasks from PowerShell

Jerod Johnson
Jerod Johnson
Director, Technology Evangelism
Are you in search of a quick and easy way to access SFTP data from PowerShell? This article demonstrates how to utilize the SFTP Cmdlets for tasks like connecting to SFTP data, automating operations, downloading data, and more.

The CData ADO.NET Provider for SFTP is a standard ADO.NET Provider that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to SFTP.

ADO.NET Provider

The ADO.NET Provider provides a SQL interface for SFTP; this tutorial shows how to use the Provider to create, retrieve, update, and delete SFTP data.

Once you have acquired the necessary connection properties, accessing SFTP data in PowerShell can be enabled in three steps.

SFTP can be used to transfer files to and from SFTP servers using the SFTP Protocol. To connect, specify the RemoteHost;. service uses the User and Password and public key authentication (SSHClientCert). Choose an SSHAuthMode and specify connection values based on your selection.

Set the following connection properties to control the relational view of the file system:

  • RemotePath: Set this to the current working directory.
  • TableDepth: Set this to control the depth of subfolders to report as views.
  • FileRetrievalDepth: Set this to retrieve files recursively and list them in the Root table.
Stored Procedures are available to download files, upload files, and send protocol commands. See gdatamodel for more on using SQL to interact with the server.
  1. Load the provider's assembly:

    
    [Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for SFTP\lib\System.Data.CData.SFTP.dll")
        
  2. Connect to SFTP:

     
    $conn= New-Object System.Data.CData.SFTP.SFTPConnection("RemoteHost=MyFTPServer;")
    $conn.Open()
    
  3. Instantiate the SFTPDataAdapter, execute an SQL query, and output the results:

    
    $sql="SELECT Filesize, Filename from MyDirectory"
    
    $da= New-Object System.Data.CData.SFTP.SFTPDataAdapter($sql, $conn)
    $dt= New-Object System.Data.DataTable
    $da.Fill($dt)
    
    $dt.Rows | foreach {
    	Write-Host $_.filesize $_.filename
    }
      

Update SFTP Data


$cmd =  New-Object System.Data.CData.SFTP.SFTPCommand("UPDATE MyDirectory SET FilePath='/documents/doc.txt' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.SFTP.SFTPParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()

Insert SFTP Data


$cmd =  New-Object System.Data.CData.SFTP.SFTPCommand("INSERT INTO MyDirectory (FilePath) VALUES (@myFilePath)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.SFTP.SFTPParameter("@myFilePath","/documents/doc.txt")))
$cmd.ExecuteNonQuery()

Delete SFTP Data


$cmd =  New-Object System.Data.CData.SFTP.SFTPCommand("DELETE FROM MyDirectory WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.SFTP.SFTPParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()

Ready to get started?

Download a free trial of the SFTP Data Provider to get started:

 Download Now

Learn more:

SFTP Icon SFTP ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with SFTP data including Files, Directories, and more!