Skip to content

Instantly share code, notes, and snippets.

@RalfWenzel
Last active October 31, 2017 20:33
Show Gist options
  • Select an option

  • Save RalfWenzel/2949a2b9553dd37b38855478d1353961 to your computer and use it in GitHub Desktop.

Select an option

Save RalfWenzel/2949a2b9553dd37b38855478d1353961 to your computer and use it in GitHub Desktop.
How to use PoSH-SSH with MS-PowerShell for administrative tasks on remote machines

How to use PoSH-SSH with MS-PowerShell for administrative tasks on remote machines

Executing basic SSH tasks using PoSH-SSH on Windows 10.

Note: PoSH-SSH is no replacement for a standard interactive ssh sessions. Better use products like Putty, MobaXterm or use buildin ssh support of the Windows Subsystem for Linux (WSL). Have a look at the documentation and use-cases below to find out if PoSH-SSH fits your requirements.

Getting started

Prerequisites

  • Windows 10
  • MS Powershell

Installing using NuGet Provider

PS C:\users\wer> Find-Module PoSH-SSH

Version    Name                                Repository           Description                                                                        
-------    ----                                ----------           -----------                                                                        
2.0.2      Posh-SSH                            PSGallery            Provide SSH and SCP functionality for executing commands against remote hosts.     

PS C:\users\wer> Find-Module PoSH-SSH | install-module

Verify installation and getting help

A list of avialable commands commands is displayed with 'get-command -module posh-ssh'.

Help for each command is available usong using get-help <CommandName> [-Detailed]

PS C:\users\wer> get-command -module posh-ssh

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        Get-PoshSSHModVersion                              2.0.2      Posh-SSH                         
Function        Get-SFTPChildItem                                  2.0.2      Posh-SSH                         
Function        Get-SFTPContent                                    2.0.2      Posh-SSH                         
Function        Get-SFTPLocation                                   2.0.2      Posh-SSH                         
Function        Get-SFTPPathAttribute                              2.0.2      Posh-SSH                         
Function        Get-SFTPSession                                    2.0.2      Posh-SSH                         
Function        Get-SSHPortForward                                 2.0.2      Posh-SSH                         
Function        Get-SSHSession                                     2.0.2      Posh-SSH                         
Function        Get-SSHTrustedHost                                 2.0.2      Posh-SSH                         
Function        Invoke-SSHCommand                                  2.0.2      Posh-SSH                         
Function        Invoke-SSHCommandStream                            2.0.2      Posh-SSH                         
Function        Invoke-SSHStreamExpectAction                       2.0.2      Posh-SSH                         
Function        Invoke-SSHStreamExpectSecureAction                 2.0.2      Posh-SSH                         
Function        New-SFTPFileStream                                 2.0.2      Posh-SSH                         
Function        New-SFTPItem                                       2.0.2      Posh-SSH                         
Function        New-SFTPSymlink                                    2.0.2      Posh-SSH                         
Function        New-SSHDynamicPortForward                          2.0.2      Posh-SSH                         
Function        New-SSHLocalPortForward                            2.0.2      Posh-SSH                         
Function        New-SSHRemotePortForward                           2.0.2      Posh-SSH                         
Function        New-SSHShellStream                                 2.0.2      Posh-SSH                         
Function        New-SSHTrustedHost                                 2.0.2      Posh-SSH                         
Function        Remove-SFTPItem                                    2.0.2      Posh-SSH                         
Function        Remove-SFTPSession                                 2.0.2      Posh-SSH                         
Function        Remove-SSHSession                                  2.0.2      Posh-SSH                         
Function        Remove-SSHTrustedHost                              2.0.2      Posh-SSH                         
Function        Rename-SFTPFile                                    2.0.2      Posh-SSH                         
Function        Set-SFTPContent                                    2.0.2      Posh-SSH                         
Function        Set-SFTPLocation                                   2.0.2      Posh-SSH                         
Function        Set-SFTPPathAttribute                              2.0.2      Posh-SSH                         
Function        Start-SSHPortForward                               2.0.2      Posh-SSH                         
Function        Stop-SSHPortForward                                2.0.2      Posh-SSH                         
Function        Test-SFTPPath                                      2.0.2      Posh-SSH                           
Cmdlet          Get-SCPFile                                        2.0.2      Posh-SSH
Cmdlet          Get-SCPFolder                                      2.0.2      Posh-SSH
Cmdlet          Get-SFTPFile                                       2.0.2      Posh-SSH
Cmdlet          New-SFTPSession                                    2.0.2      Posh-SSH
Cmdlet          New-SSHSession                                     2.0.2      Posh-SSH
Cmdlet          Set-SCPFile                                        2.0.2      Posh-SSH 
Cmdlet          Set-SCPFolder                                      2.0.2      Posh-SSH
Cmdlet          Set-SFTPFile                                       2.0.2      Posh-SSH

Basic Use Cases

About credentials / authentication

Most of the commands require the input of username/password credentials. For non-interactive use, create a a PSCredential object and pass it to the command using the -credential option The username is the login-name on the remote site.

When passwort authentication is used on remote machine, the passwort is the password the user logs in with. When keyfile authentication is used, the password is the password of the keyfile.

Copy local file to remote machine (SET-) and vice versa (GET-)

(Set-SCPFile | Get-SCPFile) -ComputerName <hostnameOrIp> -LocalFile <localFileName> -RemoteFile <remoteFileName>

Example:

Set-SCPFile -ComputerName myCloudServer -LocalFile c:\tmp\json.config -RemoteFile /tmp/json.config

Note: Use Get-SCPFolder to copy from remote to local folder

Copy local folder to remote folder and vice versa using SCP with keyfile authentication

(Set-SCPFolder | Get-SCPFolder) -ComputerName <hostnameOrIp> -KeyFile <keyFile>  ` 
        -LocalFolder <windowsPath> -RemoteFolder <linuxPath> 

Example:

Set-SCPFolder -ComputerName myCloudServer -KeyFile ~/.ssh/id_rsa  -LocalFolder c:\tmp\deploy `   
   -RemoteFolder ~/deploy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment