Model Context Protocol (MCP) finally gives AI models a way to access the business data needed to make them really useful at work. CData MCP Servers have the depth and performance to make sure AI has access to all of the answers.
Try them now for free →Automate FTP Integration Tasks from PowerShell
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 Cmdlets for FTP are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to FTP.
PowerShell Cmdlets or ADO.NET Provider?
The Cmdlets are not only a PowerShell interface to FTP, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete FTP data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for FTP. To access FTP data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for FTP.
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.
PowerShell
-
Install the module:
Install-Module FTPCmdlets
-
Connect:
$ftp = Connect-FTP -RemoteHost "$RemoteHost"
-
Search for and retrieve data:
$filepath = "/documents/doc.txt" $mydirectory = Select-FTP -Connection $ftp -Table "MyDirectory" -Where "FilePath = `'$FilePath`'" $mydirectory
You can also use the Invoke-FTP cmdlet to execute SQL commands:
$mydirectory = Invoke-FTP -Connection $ftp -Query 'SELECT * FROM MyDirectory WHERE FilePath = @FilePath' -Params @{'@FilePath'='/documents/doc.txt'}
ADO.NET
-
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
PowerShell
Update-FTP -Connection $FTP -Columns @('Filesize','Filename') -Values @('MyFilesize', 'MyFilename') -Table MyDirectory -Id "MyId"
ADO.NET
$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
PowerShell
Add-FTP -Connection $FTP -Table MyDirectory -Columns @("Filesize", "Filename") -Values @("MyFilesize", "MyFilename")
ADO.NET
$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
PowerShell
Remove-FTP -Connection $FTP -Table "MyDirectory" -Id "MyId"
ADO.NET
$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