Cmdlets for SFTP

Build 21.0.7930

Establishing a Connection

With the CData Cmdlets users can install a data module, set the connection properties, and start scripting. This section provides examples of using our SFTP Cmdlets with native PowerShell cmdlets, like the CSV import and export cmdlets.

Installing and Connecting

If you have PSGet, installing the cmdlets can be accomplished from the PowerShell Gallery with the following command. You can also obtain a setup from the CData site.

Install-Module SFTPCmdlets

The following line is then added to your profile, loading the cmdlets on the next session:

Import-Module SFTPCmdlets;

You can then use the Connect-SFTP cmdlet to create a connection object that can be passed to other cmdlets:

$conn = Connect-SFTP -RemoteHost 'MySFTPServer'

The CData Cmdlets PowerShell Module for SFTP allows connecting to SFTP servers.

Connecting to an SFTP Server

SFTP can be used to transfer files to and from SFTP servers using the SFTP Protocol. To connect, specify the RemoteHost. SFTP uses the User and Password and public key authentication (SSHClientCert). Choose an SSHAuthMode and specify connection values based on your selection.

Connecting to Data

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 Data Model for more on using SQL to interact with the server.

Retrieving Data

The Select-SFTP cmdlet provides a native PowerShell interface for retrieving data:

$results = Select-SFTP -Connection $conn -Table "Notes" -Columns @("Filesize, Filename") -Where "FilePath='/documents/doc.txt'"
The Invoke-SFTP cmdlet provides an SQL interface. This cmdlet can be used to execute an SQL query via the Query parameter.

Piping Cmdlet Output

The cmdlets return row objects to the pipeline one row at a time. The following line exports results to a CSV file:

Select-SFTP -Connection $conn -Table Notes -Where "FilePath = '/documents/doc.txt'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myNotesData.csv -NoTypeInformation

You will notice that we piped the results from Select-SFTP into a Select-Object cmdlet and excluded some properties before piping them into an Export-CSV cmdlet. We do this because the CData Cmdlets append Connection, Table, and Columns information onto each row object in the result set, and we do not necessarily want that information in our CSV file.

However, this makes it easy to pipe the output of one cmdlet to another. The following is an example of converting a result set to JSON:

PS C:\> $conn  = Connect-SFTP -RemoteHost 'MySFTPServer'
PS C:\> $row = Select-SFTP -Connection $conn -Table "Notes" -Columns (Filesize, Filename) -Where "FilePath = '/documents/doc.txt'" | select -first 1
PS C:\> $row | ConvertTo-Json
  "Connection":  {

  "Table":  "Notes",
  "Columns":  [

  "Filesize":  "MyFilesize",
  "Filename":  "MyFilename"

Deleting Data

The following line deletes any records that match the criteria:

Select-SFTP -Connection $conn -Table Notes -Where "FilePath = '/documents/doc.txt'" | Remove-SFTP

Updating Data

The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into SFTP, checking first whether a record already exists and needs to be updated instead of inserted.

Import-Csv -Path C:\MyNotesUpdates.csv | %{
  $record = Select-SFTP -Connection $conn -Table Notes -Where ("Id = `'"+$_.Id+"`'")
    Update-SFTP -Connection $conn -Table Notes -Columns @("Filesize","Filename") -Values @($_.Filesize, $_.Filename) -Where "Id  = `'$_.Id`'"
    Add-SFTP -Connection $conn -Table Notes -Columns @("Filesize","Filename") -Values @($_.Filesize, $_.Filename)

Copyright (c) 2021 CData Software, Inc. - All rights reserved.
Build 21.0.7930