Tag Archives: SSH

SSH from PowerShell with Posh-SSH

For more information on Posh-SSH:  https://github.com/darkoperator/Posh-SSH

This module is for Windows PowerShell 3.0 or above. It is compiled for .NET Framework 4.5.

Install-Module -Name Posh-SSH

Here’s a simple script that grabs credentials from a file.  See this other article for more on password management.  It uses the credentials to open a new SSH session to the remote computer and execute the pwd command and returns the results of that command.  Then, it closes the SSH session.

$computerName = "computername.domain.com"
$userId = "myUserId"
$pwd = Get-Content "$PSScriptRoot\$userId.Pw.txt" | ConvertTo-SecureString
$creds = New-Object System.Management.Automation.PSCredential -ArgumentList $userId, $pwd

try {
    "Attempting SSH to $computerName"
    $sshSession = New-SSHSession -ComputerName $computerName -Credential $creds -AcceptKey -ConnectionTimeout 10 -ErrorAction Stop
    $sessionId = $sshSession.SessionId
    "Session $sessionId opened."
    $command = "pwd"
    "Command: {0}" -f $command
    $sshOut = (Invoke-SSHCommand -SessionId $sessionId -Command $command).Output
    "Results: '{0}'" -f $sshOut
    Remove-SSHSession -SessionId $sessionId | Out-Null
    "Session $sessionId closed."
}
catch {$_.exception.Message}

Use SCP to copy files from Linux using PowerShell

There’s a lot of different tools out there to essentially SSH to a Linux host.  They seem to come short when one needs to use secure copy.  WinSCP has been around forever and is a relatively mature tool for doing SCP on a Windows client.  My requirement is to copy files from a Linux server to a Windows client.  WinSCP seems to be the best fit for my one need.  Here are the general steps to getting it done:

  • First install WinSCP wrapper to enable use of SCP from PowerShell.  Windows Management Framework 5.0 is a prerequisite.
  • Run the WinSCP application to collect the key from the target Linux host.
  • Generate credentials to be used by a script to copy files.  Run a script to copy the files.
  • If needed, set up a scheduled task to run the copy script on an interval.

Download WinSCP .NET assembly for use with PowerShell: https://winscp.net/eng/docs/library_powershell#powershell_scripting

In PowerShell running as administrator, run the following:

Install-Module -Name WinSCP

Check the install by running the following:

Get-Command -Module WinSCP

Store credentials to be used in a later script.
Save Password to File
$credential = Get-Credential
$credential.Password | ConvertFrom-SecureString | Set-Content G:\scripts\PasswordFile.txt

Run a script the uses the password file to access the Linux server and copy over your files.
$session = New-WinSCPSession -Hostname <Linux Host> -Credential $credential -SshHostKeyFingerprint "ssh-rsa-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx"

Receive-WinSCPItem -WinSCPSession $session -Path “/…/somefile” -Destination “C:\folder\destination\etc\”

Remove-WinSCPSession -WinSCPSession $session