【问题标题】:Powershell start executable that requires to type "Y"Powershell 启动需要输入“Y”的可执行文件
【发布时间】:2015-10-27 17:36:21
【问题描述】:

在 powershell 脚本中,我必须运行外部命令。

此命令是一个命令行可执行文件,需要通过键入Y ( Do you want to proceed? [Y or N] then press [enter]) 来确认其执行。

很遗憾,没有静音开关。

如何运行命令,等待此确认并输入Y 代替用户?

我试过了:

$psi = New-Object System.Diagnostics.ProcessStartInfo
$psi.FileName = "\\path\to\my\executable.exe"
$psi.UseShellExecute = $false
$psi.RedirectStandardInput = $true
$psi.RedirectStandardOutput = $true
$psi.Arguments = '-import -scope "\\path\to\my\scope.xml"'


$p = [System.Diagnostics.Process]::Start($psi)

$line = ""
do{
    while($line = $p.StandardOutput.ReadLine()){
        Write-Host $line

        if($line -Eq "[Y or N] then press [enter]:"){
            $p.StandardInput.WriteLine("Y")
        }
    }
} while(-not $p.HasExited)

但看起来进程立即退出。输出只是我的命令的第一行。

其实我应该是:

Search Migration Tool v1.2.1
Program Action: Import
SharePoint objects considered: Scopes
Conflict Behavior: Continue
Filename: \\path\to\my\scope.xml

Do you want to proceed?
[Y or N] then press [enter]:

但只显示Search Migration Tool v1.2.1

【问题讨论】:

  • 你试过了吗:'Y'|&"\\path\to\my\executable.exe"
  • @PetSerAl:有效!你应该把它写成答案

标签: powershell


【解决方案1】:

在大多数情况下,您不必等待Do you want to proceed? 请求。您只需在应用程序的输入流中编写一些内容,然后应用程序在应用程序需要时将其拾取。在最简单的情况下,您只需将某些内容通过管道传输到应用程序,然后将其写入应用程序的输入流,因此您甚至不必直接使用 Process 类:

'Y'|&"\\path\to\my\executable.exe"

【讨论】:

  • 一个非常简单的解决方案的完美示例,而我却沉浸在一个复杂的解决方案中。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-31
相关资源
最近更新 更多