Ready to get started?

Download a free trial of the SFTP Data Provider to get started:

 Download Now

Learn more:

SFTP Icon SFTP ADO.NET Provider

An easy-to-use database-like interface for .NET applications access to remote files and directories.

Automate SFTP Integration Tasks from PowerShell



Are you in search of a quick and easy way to access SFTP data from PowerShell? This article demonstrates how to utilize the SFTP Cmdlets for tasks like connecting to SFTP data, automating operations, downloading data, and more.

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.

PowerShell Cmdlets or ADO.NET Provider?

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:

  • 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.
Stored Procedures are available to download files, upload files, and send protocol commands. See gdatamodel for more on using SQL to interact with the server.

PowerShell

  1. Install the module:

    Install-Module SFTPCmdlets
  2. Connect:

    $sftp = Connect-SFTP -RemoteHost "$RemoteHost"
  3. 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'}

ADO.NET

  1. Load the provider's assembly:

    [Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for SFTP\lib\System.Data.CData.SFTP.dll")
  2. Connect to SFTP:

    $conn= New-Object System.Data.CData.SFTP.SFTPConnection("RemoteHost=MyFTPServer;") $conn.Open()
  3. 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

PowerShell

Update-SFTP -Connection $SFTP -Columns @('Filesize','Filename') -Values @('MyFilesize', 'MyFilename') -Table MyDirectory -Id "MyId"

ADO.NET

$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

PowerShell

Add-SFTP -Connection $SFTP -Table MyDirectory -Columns @("Filesize", "Filename") -Values @("MyFilesize", "MyFilename")

ADO.NET

$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

PowerShell

Remove-SFTP -Connection $SFTP -Table "MyDirectory" -Id "MyId"

ADO.NET

$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()