使用 System.Diagnostics.Process 在标准输入上将 y 输入到 Plink? [英] Using System.Diagnostics.Process to input a y to a Plink on standard input?

查看:42
本文介绍了使用 System.Diagnostics.Process 在标准输入上将 y 输入到 Plink?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 PowerShell 中将 Y 传递给由 System.Diagnostic.Process 启动的进程?

How do I pass a Y into a process started by a System.Diagnostic.Process in PowerShell?

function Start-NewPlinkProcess(
        [string]$pfile = 'plink.exe',
        [string]$arguments = 'somehost -l somelogin -pw somepasswd ping -c 12 someOtherHost > /home/homeie/mePingTestResults.txt'
    ){
    $p = New-Object System.Diagnostics.Process;
    $p.StartInfo.UseShellExecute = $false;
    $p.StartInfo.RedirectStandardOutput = $true;
    $p.StartInfo.RedirectStandardInput = $true;
    $p.StartInfo.FileName = $pfile;
    $p.StartInfo.Arguments = $arguments
    $p.StandardInput.WriteLine("Y") # Pass a Y to stdin ignore that...
    $pident = ($p.Start()).Id
    Write-Host("pid: $($pident)");
    #$p.WaitForExit();
    #$p.StandardOutput.ReadToEnd();
    return $p
}

当我调用它时,我仍然得到:

When I call it I still get:

If you trust this host, enter "y" to add the key to
PuTTY's cache and carry on connecting.
If you want to carry on connecting just once, without
adding the key to the cache, enter "n".
If you do not trust this host, press Return to abandon the
connection.
Store key in cache? (y/n)

我在别处读到可以尝试像 echo y | 这样的东西.plink ... 并让它从标准输入通过管道读取它,但我想对它有更多的控制.

I've read elsewhere that it's possible to try something like echo y | plink ... and have it read it in piped from standard input, but I want to have more control over it then just that.

推荐答案

只需将 StandardInput 行移动到流程开始的下方即可.

Just move your StandardInput line below where the process is started.

function Start-NewPlinkProcess(
        [string]$pfile = 'plink.exe',
        [string]$arguments = 'somehost -l somelogin -pw somepasswd ping -c 12 someOtherHost > /home/homeie/mePingTestResults.txt'
    ){
    $p = New-Object System.Diagnostics.Process;
    $p.StartInfo.UseShellExecute = $false;
    $p.StartInfo.RedirectStandardOutput = $true;
    $p.StartInfo.RedirectStandardInput = $true;
    $p.StartInfo.FileName = $pfile;
    $p.StartInfo.Arguments = $arguments
    $pident = ($p.Start()).Id
    Write-Host("pid: $($pident)");
    $p.StandardInput.WriteLine("Y") # Pass a Y to stdin ignore that...
    #$p.WaitForExit();
    #$p.StandardOutput.ReadToEnd();
    return $p
}

这篇关于使用 System.Diagnostics.Process 在标准输入上将 y 输入到 Plink?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆