How to Pass a Password (and Enter press) to a Prompt in Powershell

Where I’m working at-the-moment, I don’t have access to plink.exe, nor some other tools I’d normally use, but I can get to PowerShell, and the desktop environment has ssh.exe. The problem with ssh.exe is that there’s no command line switch to allow for a password. So, here’s how I can make PowerShell automatically answer ssh.exe’s password rompt.

1) Set the PowerShell Window Title to something specific:

$host.ui.RawUI.WindowTitle = "Your Title"

2) Save a script called say “SendPassword.ps1” with the below content to somewhere accessible (I’ll use 'C:\MY TOOLS' in this post):

$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('Your Title)
Sleep 4
$wshell.SendKeys('YourPassword')
$wshell.SendKeys('~')

Note: You will need to tune the sleep timer so that it finishes after the ‘Password’ prompt appears. 4 seconds worked for me in the example below.

3) Run your SSH command from PowerShell like below:

start powershell.exe '.\SendPassword.ps1' -WorkingDirectory 'C:\MY TOOLS'; ssh.exe admin@10.0.0.200 node show local -fields serialnumber

Image: Screenshot of the setup

Comments