Saturday, August 29, 2015

PowerShell Password-less SSH to Linux

Using Powershell version 3:

PS C:\Windows\system32> $PSVersionTable
Name                           Value
----                           -----
WSManStackVersion              3.0
PSCompatibleVersions           {1.0, 2.0, 3.0}
BuildVersion                   6.2.9200.16398
PSVersion                      3.0
CLRVersion                     4.0.30319.1
PSRemotingProtocolVersion      2.2

PS C:\Windows\system32> $PSVersionTable.PSVersion
Major  Minor  Build  Revision
-----  -----  -----  --------
3      0      -1     -1
Download SSH-Session module from:
In Download Section:
Extract and this creates:

Get the Module Paths:

PS C:\Windows\system32> $env:PSModulePath

This is module extention path: C:\Users\rumman\Documents\WindowsPowerShell\Modules
Copy "C:\Downloads\SSH-SessionsPSv3\SSH-Sessions" To "C:\Users\rumman\Documents\WindowsPowerShell\Modules"

Now, in shell prompt, use the following command to ensure that the module is available:
PS C:\Windows\system32> Get-Module *ssh*
ModuleType Name                                ExportedCommands
---------- ----                                ----------------
Script     SSH-Sessions                        {ConvertFrom-SecureToPlain, Enter-SshSession, Get-SshSession, 
PS C:\Windows\system32> Get-Command *ssh*
CommandType     Name                                               ModuleName
-----------     ----                                               ----------
Function        Enter-SshSession                                   SSH-Sessions
Function        Get-SshSession                                     SSH-Sessions
Function        Invoke-SshCommand                                  SSH-Sessions
Function        New-SshSession                                     SSH-Sessions
Function        Remove-SshSession                                  SSH-Sessions

Import Module:

PS C:\Users\rumman> Import-Module SSH-Sessions
Import-Module : Could not load file or assembly
'file:///C:\Users\rumman\Documents\WindowsPowerShell\Modules\SSH-Sessions\Renci.SshNet.dll' or one of its
dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)
At line:1 char:1
+ Import-Module SSH-Sessions
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Import-Module], FileLoadException
    + FullyQualifiedErrorId : FormatXmlUpdateException,Microsoft.PowerShell.Commands.ImportModuleCommand

I faced the above issue and what I did unblocking the file.
Go to C:\Users\rumman\Documents\WindowsPowerShell\Modules\SSH-Sessions
There are 3 files:
- Renci.SshNet.dll
- SSH-Sessions.psd1
- SSH-Sessions.psm1

For each file, right-click -> Properties -> Unblock
Then, load the module again:
PS C:\Users\rumman> Import-Module SSH-Sessions
PS C:\Users\rumman>

Verify SSH Connection:

PS C:\Users\rumman> New-SSHSession -ComputerName "" -Username "postgres"
No key provided. Enter SSH password for postgres: ******
Successfully connected to
PS C:\Users\rumman> Get-Session

PS C:\Users\rumman> Get-SSHSession
ComputerName                                                                                                  Connected
------------                                                                                                  ---------                                                                                                          True

PS C:\Users\rumman> Remove-SSHSession -ComputerName "" should now be disconnected and disposed.

Setup Password-less SSH:

Download Putty.exe and Puttygen.exe. One of the sites for this:

There are lots of sites available to describe the steps to use puttygen to setup password-less ssh.
For example,
I saved the private key at  C:\putty\private_key.ppk
And also use the menu conversion->export OpenSSH
and save it as exported_openssh_key
This key is usable for other application.

When password-less ssh is working form Putty, now try from powershell:
PS C:\Users\rumman> New-SSHSession -ComputerName "" -UserName "postgres" -KeyFile C:\putty\exported_openssh_key
Key file specified. Will override password. Trying to read key file...
PS C:\Users\rumman> Get-SSHSession
ComputerName                                                                                                  Connected
------------                                                                                                  ---------                                                                                                          True

2 Responses so far

  1. karthik says:

    Very good information. Its very useful for me. We need learn from real time examples and for this we choose good training institute, we need to learn from experts . So we make use of demo classes . Recently we tried linux demo class of Apponix Technologies.

  2. Unknown says:

    Buy your products online at low rates in Auckland Newzealand.We Provide you quality household items, outdoor furniture nz, gazeboz nz, items and all the items at low rates.

Leave a Reply