![]() |
VOOZH | about |
The CData Cmdlets for SFTP 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 SFTP.
The Cmdlets are not only a PowerShell interface to SFTP, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete SFTP data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for SFTP. To access SFTP data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for SFTP.
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:
Install the module:
Install-Module SFTPCmdlets
Connect:
$sftp = Connect-SFTP -RemoteHost "$RemoteHost"
Search for and retrieve data:
$filepath = "/documents/doc.txt" $mydirectory = Select-SFTP -Connection $sftp -Table "MyDirectory" -Where "FilePath = `'$FilePath`'" $mydirectory
You can also use the Invoke-SFTP cmdlet to execute SQL commands:
$mydirectory = Invoke-SFTP -Connection $sftp -Query 'SELECT * FROM MyDirectory WHERE FilePath = @FilePath' -Params @{'@FilePath'='/documents/doc.txt'}
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 -Connection $SFTP -Columns @('Filesize','Filename') -Values @('MyFilesize', 'MyFilename') -Table MyDirectory -Id "MyId"
$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()
Add-SFTP -Connection $SFTP -Table MyDirectory -Columns @("Filesize", "Filename") -Values @("MyFilesize", "MyFilename")
$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()
Remove-SFTP -Connection $SFTP -Table "MyDirectory" -Id "MyId"
$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
Download a free trial of the SFTP Data Provider to get started:
Download NowLearn more:
👁 SFTP IconAn easy-to-use database-like interface for .NET applications access to remote files and directories.