Create a process using Plink in PowerShell and sending commands to it

Programmer_D picture Programmer_D · Nov 24, 2013 · Viewed 15.8k times · Source

I need to be able to create a process on a remote device and send come commands to it using a PowerShell script. I need to do it this way because I get prompts when I use a command. I have been trying to figure out how to do this by using Google searches and the following link: http://tartarus.org/~simon/putty-snapshots/htmldoc/Chapter7.html#plink. From that link, I realized using a file with a list of commands in Plink won't work for the following reason (copied and pasted from that link):

Any non-interactive command you could usefully run on the server command line, you can run in a batch file using Plink in this way.

It says "non-interactive command", which is what I'm using. I have also tried using a file with a list of commands anyway, but it didn't solve my problem because I basically need to give a command, wait, and then give another one when it prompts me. This is what I found in the PuTTY FAQ and basically what I would like to try:

Probably your best bet is to use Plink, the command-line connection tool. If you can start Plink as a second Windows process, and arrange for your primary process to be able to send data to the Plink process, and receive data from it, through pipes, then you should be able to make SSH connections from your program. This is what CVS for Windows does, for example.

EDIT: Based on the answer from user2460798, I have tried out the solution below. I'm hoping to use PuTTY and be able to send commands to it that way. My problem now is that I don't know how to send commands to the PuTTY session that gets opened. So basically this code opens up a PuTTY session and tries to write the "ls" command to it, but nothing happens. I have no idea where the "ls" text is going.

$procInfo = New-Object Diagnostics.ProcessStartInfo
$procInfo.RedirectStandardOutput=$true
$procInfo.RedirectStandardInput=$true
$procInfo.RedirectStandardError=$true
$procInfo.FileName="putty.exe"
$procInfo.Arguments="-ssh -pw <password> <username>@<device>"
$procInfo.UseShellExecute=$false
$p=[diagnostics.process]::start($procInfo)
sleep -Milliseconds 3000
$p.StandardInput.WriteLine("ls")

Answer

JPBlanc picture JPBlanc · Nov 25, 2013

Not sure to understand your question, but here is the way I use plink

function plink
{
  [CmdletBinding()]
  PARAM
  (
    [Parameter(Mandatory=$True)]
    [ValidateNotNullOrEmpty()]
    [string] $remoteHost,

    [Parameter(Mandatory=$True)]
    [ValidateNotNullOrEmpty()]
    [string] $login,

    [Parameter(Mandatory=$True)]
    [ValidateNotNullOrEmpty()]
    [string] $passwd,

    [Parameter(Mandatory=$True)]
    [ValidateNotNullOrEmpty()]
    [string] $command)

  & c:\Tools\plink.exe -ssh $remoteHost -l $login -pw $passwd $command
  return
}

$remoteHost = "192.168.1.1"
$login = "TheUserWhoCan"

$command1 = "/opt/compiere/apache-tomcat-6.0.32/bin/shutdown.sh "
$command2 = "cd /opt/compiere/apache-tomcat-6.0.32/bin && ./startWS.sh"
$command3 = "ps -edalf | grep java"

$passwd = Read-Host "Enter Password for $login"



write-host "Stopping tomcat" -ForegroundColor DarkGreen
plink -remoteHost $remoteHost -login compiere -command $command1 -passwd $passwd
Start-Sleep 10
write-host "Starting tomcat" -ForegroundColor DarkGreen
plink -remoteHost $remoteHost -login compiere -command $command2 -passwd $passwd

write-host "Looking for java processes"-ForegroundColor DarkGreen
plink -remoteHost $remoteHost -login compiere -command $command3 -passwd $passwd