3

If I want to overwrite existing SSH key file without typing y, then I would do this in bash:

echo "y" | ssh-keygen -f /home/wakatana/id_rsa -N ""

How can same be achieved using PowerShell? I've tried following:

Write-Host "y" | ssh-keygen.exe -f C:\Users\wakatana\Desktop\id_rsa -N """"

It won't show me Overwrite (y/n)? dialog, but I does not rewrite existing key. What is the problem?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Wakan Tanka
  • 7,542
  • 16
  • 69
  • 122

3 Answers3

3

Just delete the file if it already exists, before calling ssh-keygen.

Blindly sending "yes" to an application, without knowing, what does it ask in the first place, is quite dangerous.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
1

Why not just rm the existing keys first?

Powershell (not a ps guru, so adjust to taste):

Remove-Item -Path C:\Users\wakatana\Desktop\id_rsa && Remove-Item -Path C:\Users\wakatana\Desktop\id_rsa.pub && ssh-keygen.exe -f C:\Users\wakatana\Desktop\id_rsa -N """"

Linux (since I'm here and this is what I came for):

rm -f path/to/.ssh/id_rsa* && ssh-keygen -b 4096 -t rsa -f path/to/.ssh/id_rsa

JIC any of this assists the next one to stop by. Keep on keepin' on, fellow devs =]

Edit: I read good. Just caught Martin Prikryl's answer. Same diff - hah!

Rohjay
  • 101
  • 1
  • 7
0

Try Write-Output instead of Write-Host:

Write-Output "y" | ssh-keygen.exe -f .\test -N "blahblah"

enter image description here

Glenn
  • 1,687
  • 15
  • 21
  • 1
    `Write-Output` is superfluous here. – Bill_Stewart Dec 30 '19 at 18:52
  • Good point by @Bill_Stewart . This should work as well: `"y" | ssh-keygen.exe -f .\test -N "blahblah"` – Glenn Dec 30 '19 at 19:43
  • thank you very much. This worked. Can you please explain why `Write-Host` is not working while `Write-Output` is? – Wakan Tanka Jan 03 '20 at 12:23
  • @WakanTanka [here](https://stackoverflow.com/questions/8755497/whats-the-difference-between-write-host-write-output-or-consolewrite) is a good explanation. In my own words, `Write-Host` send output to the "host", which can vary, but is usually the command console. `Write-Output` sends the output through the pipeline and to STDOUT, which is then interpreted as the STDIN for the next native command in the pipeline. – Glenn Jan 03 '20 at 13:10