Automate FTP Integration Tasks from PowerShell

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

The CData ADO.NET Provider for FTP 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 FTP.

ADO.NET Provider

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

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

To connect to FTP or SFTP servers, specify at least RemoteHost and FileProtocol. Specify the port with RemotePort.

Set User and Password to perform Basic authentication. Set SSHAuthMode to use SSH authentication. See the Getting Started section of the data provider help documentation for more information on authenticating via SSH.

Set SSLMode and SSLServerCert to secure connections with SSL.

The data provider lists the tables based on the available folders in your FTP server. 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 folders to list as views.
  • FileRetrievalDepth: Set this to retrieve and list files recursively from the root table.

Stored Procedures are available to download files, upload files, and send protocol commands. See the Data Model chapter of the FTP data provider documentation for more information.

  1. Load the provider's assembly:

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

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

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

Update FTP Data


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

Insert FTP Data


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

Delete FTP Data


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

Ready to get started?

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

 Download Now

Learn more:

FTP Icon FTP ADO.NET Provider

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