Enable everyone in your organization to access their data in the cloud — no code required.
Learn More →Automate FTP Integration Tasks from PowerShell
Are you looking for a quick and easy way to access FTP data from PowerShell? We show how to use the Cmdlets for FTP and the CData ADO.NET Provider for FTP to connect to FTP data and synchronize, automate, download, 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.
Cmdlets or ADO.NET?
The cmdlets are not only a PowerShell interface to the FTP API, 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.
After obtaining the needed connection properties, accessing FTP data in PowerShell consists of three basic 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 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 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 System.Data.CData.FTP.FTPParameter("@myId","001d000000YBRseAAH"))
$cmd.ExecuteNonQuery()
CodeProject