如何在PowerShell中将Y
传递到由System.Diagnostic.Process
启动的进程中?
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
}
当我调用它时,我仍然得到:
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 ...
这样的东西并将其从标准输入中以管道方式读取,但是我想对此进行更多控制。
最佳答案
只需将您的StandardInput行移动到开始该过程的位置下方即可。
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
}
关于powershell - 使用System.Diagnostics.Process在标准输入上将y输入到Plink?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54227684/