Contents

Managing Files over SFTP with PowerShell

21 Jul 2018 2 30  0
Managing Files over SFTP with PowerShell
Managing Files over SFTP with PowerShell

 

Although native support for Secure File Transfer Protocol (SFTP) is not default in PowerShell, by using a freely available module, we can retrieve, delete, and add new files via SFTP.

Module Posh-SHH

Even though native support for Secure File Transfer Protocol (SFTP) is not available by default in PowerShell, if you use a freely available module, you can retrieve, delete, and append new files using SFTP.

The module you have to use is called Posh-SSH. This module is available on the PowerShell Gallery and it can be installed by running Install-Module –Name Posh-SSH. After It’s installed, you can take a look at all of the commands available.

Get-Command -Module posh-ssh -Noun *SFTP*
CommandType           Name                                 Version    Source
-----------           ----                                 -------    ------
Function              Get-SFTPChildItem                    2.0.1      posh-ssh
Function              Get-SFTPContent                      2.0.1      posh-ssh
Function              Get-SFTPLocation                     2.0.1      posh-ssh
Function              Get-SFTPPathAttribute                2.0.1      posh-ssh
Function              Get-SFTPSession                      2.0.1      posh-ssh
Function              New-SFTPFileStream                   2.0.1      posh-ssh
Function              New-SFTPItem                         2.0.1      posh-ssh
Function              New-SFTPSymlink                      2.0.1      posh-ssh
Function              Remove-SFTPItem                      2.0.1      posh-ssh
Function              Remove-SFTPSession                   2.0.1      posh-ssh
Function              Rename-SFTPFile                      2.0.1      posh-ssh
Function              Set-SFTPContent                      2.0.1      posh-ssh
Function              Set-SFTPLocation                     2.0.1      posh-ssh
Function              Set-SFTPPathAttribute                2.0.1      posh-ssh
Function              Test-SFTPPath                        2.0.1      posh-ssh
Cmdlet                Get-SFTPFile                         2.0.1      posh-ssh
Cmdlet                New-SFTPSession                      2.0.1      posh-ssh
Cmdlet                Set-SFTPFile                         2.0.1      posh-ssh

Right away, it looks like we should be able to do a lot with this module. Let’s dig into some of the SFTP commands and see how they are used.

Transfering Files over SFTP

To transfer files over SFTP, we have to establish a session on the SFTP server. We’ll only need to do this once. We can create a separate session every time we have to perform a certain task over SFTP, but that won’t be very efficient

Instead, we need to create a single SFTP session with the New-SFTPSession command. We will be taking the simple route and not be messing with certificates, so we can use a username and password to authenticate to the SFTP server. The New-SFTPSession has a Credential parameter which accepts a PSCredential object. We are going to use the Get-Credential command to prompt the user for a username and password.

$credential = Get-Credential

After we’ve got the username and password captured, we may then pass them to the New-SFTPSession command along with the AcceptKey parameter. The AcceptKey parameter should automatically accept the key that’s returned from the SFTP server rather than prompting you to do so.

$session = New-SFTPSession -ComputerName 'MYSFTPSERVER' -Credential $Credential -AcceptKey

If everything goes well, you will be returned to the console. If so, we may now use this session with a number of commands. For example, if we download a file from the SFTP server to our local computer, we could use the Get-SFTPFile function.

Get-SFTPFile -SessionId $session.SessionId -Path 'C:localfile.txt'

We may have to delete a file on the SFTP server. We can use the Remove-SFTPItem function for that just as easily as we use the Get-SFTPFile function.

Get-SFTPFile -SessionId $session.SessionId -Path 'C:localfile.txt'

After you are done with whatever you had to do on the SFTP server, we can then disconnect and remove the session. We could just call Remove-SFTPSession and provide the session, but it’s always better to check ahead of time just to be sure the session is still there. We could check to see if the session is still made by using the Get-SFTPSession.

You can see below that I’m first checking to see if our session exists. If so, I’m then disconnecting it and completely removing it from our session.

if ($session = Get-SFTPSession -SessionId $session.SessionId) {
    $session.Disconnect()
}
$null = Remove-SftpSession -SftpSession $session

The next time you need to perform any kind of SFTP task, have a look at the Posh-SSH PowerShell module.

 

0 Comments
Add a comment

Leave a Reply