Ready to get started?

Learn more about the CData ADO.NET Provider for SFTP or download a free trial:

Download Now

Automate SFTP Integration Tasks from PowerShell

Are you looking for a quick and easy way to access SFTP data from PowerShell? We show how to use the Cmdlets for SFTP and the CData ADO.NET Provider for SFTP to connect to SFTP data and synchronize, automate, download, 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.

Cmdlets or ADO.NET?

The cmdlets are not only a PowerShell interface to the SFTP API, 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.

After obtaining the needed connection properties, accessing SFTP data in PowerShell consists of three basic 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 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 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 System.Data.CData.SFTP.SFTPParameter("@myId","001d000000YBRseAAH")) $cmd.ExecuteNonQuery()