Scripting 相当于bash“;期望;在powershell中

Scripting 相当于bash“;期望;在powershell中,scripting,powershell,Scripting,Powershell,我正在使用powershell运行另一个powershell脚本,在某个点上,另一个脚本请求一些输入,我希望能够读取另一个脚本的输出,并基于该脚本提供的输入。类似于在bash上使用expect所能做的 有什么想法吗 谢谢我不知道本机有任何复制精确数据的功能。这个问题的答案声称能够将内容传递给流程或从流程传递内容,因此它可以处理您想要的内容 祝你好运 您只需在powershell中使用Expect程序。它起作用了。Powershell也是一个shell,您可以运行Powershell编写的代码,

我正在使用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
}
让我解释一下测试

  • 创建一个需要输入的bash文件
  • 创建一个tcl文件,该文件调用在第一步中创建的bash
  • 从powershell调用tcl程序,它可以工作,不会等待输入

  • 只是张贴我的解决方案,以便它可以帮助别人。我在运行其他脚本时遇到了同样的问题,这些脚本将询问答案。首先创建一个文件“inputFileLocation.txt”,按顺序列出每行中每个问题的答案。然后用下面的语法运行脚本。它将完成这项工作

    `cmd.exe /c "script.bat < inputFileLocation.txt"`
    
    `cmd.exe/c“script.bat
    2014年,李·霍姆斯(Lee Holmes)在名为“等待”的Powershell画廊上发表了一篇题为“期待Powershell”的文章。事实证明,模拟expect比您想象的要复杂得多,涉及Win32调用

    套餐

    演示