2011-10-17 179 views
6

我正在使用powershell来运行另一个powershell脚本,在某些时候其他脚本要求输入一些内容,我希望能够读取其他脚本的输出并基于为其提供输入。类似于你对bash的期望。相当于在PowerShell中bash“expect”

任何想法?

谢谢

回答

0

您只需在PowerShell中使用Expect程序。有用。 Powershell也是一个shell,你可以运行powershell编写的代码,它会调用bash代码,这会再次调用powershell。

波纹管是一个测试,它通过。

It "can work with tcl expect" { 
    $bs = @' 
    echo "Do you wish to install this program?" 
    select yn in "Yes" "No"; do 
    case $yn in 
     Yes) echo "install"; break;; 
     No) exit;; 
    esac 
done 
'@ 

$bsf = New-TemporaryFile 
$bs | Set-Content -Path $bsf 

$tcls = @' 
#!/bin/sh 
# exp.tcl \ 
exec tclsh "$0" ${1+"[email protected]"} 

package require Expect 
set timeout 100000 
spawn {spawn-command} 
expect { 
    "Enter password: $" { 
     exp_send "$password\r" 
     exp_continue 
    } 
    "#\? $" { 
      exp_send "1" 
    } 
    eof {} 
    timeout {} 
} 
'@ 

$tclf = New-TemporaryFile 
$tcls -replace "{spawn-command}",("bash",$bsf -join " ") | Set-Content -Path $tclf 
"bash", $tclf -join " " | Invoke-Expression 

Remove-Item $bsf 
Remove-Item $tclf 
} 

让我来解释一下这个测试。

  1. 创建一个期望输入的bash文件。
  2. 创建一个调用在第一步中创建的bash的tcl文件。
  3. 从powershell调用tcl程序,它的工作原理,不会等待输入。
0

只需发布我的解决方案,以便它可以帮助某人。我在运行一些其他需要答案的脚本时遇到同样的问题。首先创建一个文件“inputFileLocation.txt”,每行按顺序排列每个问题的答案。然后以下面的语法运行脚本。它会完成这项工作。

`cmd.exe /c "script.bat < inputFileLocation.txt"`