Automate FTP Integration Tasks from PowerShell
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.
-
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for FTP\lib\System.Data.CData.FTP.dll") -
Connect to FTP:
$conn= New-Object System.Data.CData.FTP.FTPConnection("RemoteHost=MyFTPServer;") $conn.Open() -
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()
CodeProject