Posh-SSH

This article is about PowerShell and the tasks I use it for.

Use SFTP/SSH/SCP with Posh-SSH module:

PS C:\> Install-Module -Name Posh-ssh

Interactive approvals later:

PS C:\> $username = 'whateveruser'

PS C:\> $password = ConvertTo-SecureString 'WhateverPassword' -AsPlainText -Force

PS C:\> $mycredentials = New-Object System.Management.Automation.PSCredential($username,$password)

PS C:\> New-SSHSession -computername 'whateverserver.example.org' -Credential $mycredentials -Verbose

Or generate an OpenSSH public/private key pair with no password, some notes:

PS C:\> $username = 'whateveruser'

PS C:\> $nopassword = new-object System.Security.Securestring

PS C:\> $mycredentials = New-Object System.Management.Automation.PSCredential($username,$nopassword)

PS C:\> New-SSHSession -computername 'whateverserver.example.org' -Credential $mycredentials -KeyFile C:\temp\id_rsa.private -Verbose

Other command examples:

PS C:\> Get-SSHSession | fl

PS C:\> Invoke-SSHCommand -Index 0 -Command "uname -a"

PS C:\> Remove-SSHSession -Index 0 -Verbose

PS C:\> New-SFTPSession -ComputerName 'whateverserver.example.org' -Credential (Get-Credential root) -Verbose | fl

PS C:\> Set-SFTPDirectoryPath -Index 0 -Path /usr/bin

PS C:\> Get-SFTPDirectoryList -Index 0 -Path /tmp

Other commands:

Get-SFTPFile

Move-SFTPFile

Remove-SFTPFile

Set-SFTPFile (Uploads a file)

New-SFTPDirectory

Remove-SFTPDirectory

Use SFTP/FTP/SCP/FTPS with WinSCP module:

PS C:\temp> Install-Module -Name WinSCP

Interactive approvals later:

PS C:\> $sessionOption = New-WinSCPSessionOption -hostname 'whateverserver' -Protocol 'Sftp'

PS C:\> $sshHostKeyFingerprint = Get-WinSCPHostKeyFingerprint -SessionOption $sessionOption

PS C:\> $cred = Get-Credential

PS C:\> $sessionOption = New-WinSCPSessionOption -hostname 'whateverserver' -Protocol 'Sftp' -Credential $cred -SshHostKeyFingerprint $sshHostKeyFingerprint

PS C:\> $session1 = New-WinSCPSession -SessionOption $sessionOption

PS C:\> Get-WinSCPChildItem -Path '/home/netadmin'|fl

PS C:\> Remove-WinSCPSession $session1