2011-10-17 180 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"`