4

I have generated an ssh key pair using puttygen and can successfully connect to my CentOS server with my private key from putty on my Windows 10 workstation:

image of successful connection to CentOS

I want to connect to the server from Windows PowerShell and have loaded the Posh-SSH module with Install-Module posh-ssh

I have tried to create a new ssh session with:

$Credential = Get-Credential
$KeyFile = 'C:\Users\mark\Documents\ssh\privkey.ppk'
$sesh = New-SSHSession -ComputerName neon.localdomain -Credential $credential -Keyfile $KeyFile

I put in root and blank password for Get-Credential but I get this error:

New-SSHSession : Invalid private key file.

I tried to convert the privkey file to a string by reading it and converting to base64 encoded but I get the same error:

$privkeyString = Get-Content $KeyFile
$Bytes = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($privkeyString))
$sesh = New-SSHSession -ComputerName neon.localdomain -Credential $credential -KeyString $Bytes

I also tried this but get the same error:

$sesh = New-SSHSession -ComputerName neon.localdomain -Credential $credential -KeyString $privkeystring

Any ideas on how to connect to a linux server using PowerShell with a private key file?

James C.
  • 11,076
  • 2
  • 29
  • 37
Mark Allison
  • 6,043
  • 26
  • 90
  • 140
  • This question might be a good candidate for canonical answer on similar questions like https://stackoverflow.com/questions/66539007 or https://stackoverflow.com/questions/31244250 . – Vesper Mar 09 '21 at 13:29

3 Answers3

9

New-SSHSession doesn't recognize PuTTY's key format (unfortunately neither the Gallery nor the project page mention this, but I found it in a PowerShellMagazine article). You need the private key in the OpenSSH format. You can convert the private key with PuTTYgen:

  1. Click File → Load private key.
  2. Enter the passphrase if the key is password-protected.
  3. Click Conversions → Export OpenSSH key.
  4. Enter the filename for the exported key (do NOT overwrite the PPK file) and click Save.
  5. Exit PuTTYgen.

Run New-SSHSession with the new key file:

$computer = 'neon.localdomain'
$username = 'foo'
$keyfile  = 'C:\path\to\priv_openssh.key'

$sess = New-SSHSession -Computer $computer -Credential $username -Keyfile $keyfile
Ansgar Wiechers
  • 175,025
  • 22
  • 204
  • 278
0

Same method can now be used as on Linux.

ssh username@domain.com -p 22 -i C:\Users\user.ssh\private_key_id_rsa

Goddard
  • 2,120
  • 25
  • 34
0

![enter image descrcontent://com

google.android

.apps.docs.storage

/document/ac=1 ;doc=encoded

=fsqjpfgiD4T3A RrffzMZw4PTYZT

6QpUPYqwksyw V+GU9 7GS B8X /YTY4WiUiption here](https://i.stack.imgur.com/eMNpU.png)