我正在使用 powershell 运行另一个 powershell 脚本,在某些时候另一个脚本要求一些输入,我希望能够读取来自另一个脚本的输出并基于该脚本的输入。类似于您可以在 bash 上使用 expect 执行的操作。
有任何想法吗?
谢谢
最佳答案
您只需在您的 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+"$@"}
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
}
让我解释一下这个测试。
关于scripting - 相当于powershell中的bash "expect",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7800368/