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