相当于bash的“期望”在 powershell 中

发布于 2024-12-10 04:03:36 字数 144 浏览 1 评论 0原文

我正在使用 powershell 运行另一个 powershell 脚本,在某些时候另一个脚本要求一些输入,我希望能够读取另一个脚本的输出并基于向其提供的输入。类似于您可以在 bash 上使用 Expect 执行的操作。

有什么想法吗?

谢谢

I'm using powershell to run another powershell script, at some point the other script asks for some input, I would like to be able to read the output from the other script and based on that supply input to it. Similar to what you can do with expect on bash.

Any ideas?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

忘羡 2024-12-17 04:03:36

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

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

Just posting my solution so that it can help someone. I faced the same problem while running some other script that will ask for answers. First create a file "inputFileLocation.txt" with answers to each question in each line in sequence. Then run the script in below syntax. And it will do the work.

`cmd.exe /c "script.bat < inputFileLocation.txt"`
云仙小弟 2024-12-17 04:03:36

解决部分问题的示例

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
Start-Process -FilePath C:\myexecbatchfile.bat

# Wait the application start for 2 sec 
Start-Sleep -m 2000

# Send keys
[System.Windows.Forms.SendKeys]::SendWait("input1")
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
Start-Sleep -m 3000

[System.Windows.Forms.SendKeys]::SendWait("input2")
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")

Sample to solve part of the problem

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
Start-Process -FilePath C:\myexecbatchfile.bat

# Wait the application start for 2 sec 
Start-Sleep -m 2000

# Send keys
[System.Windows.Forms.SendKeys]::SendWait("input1")
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
Start-Sleep -m 3000

[System.Windows.Forms.SendKeys]::SendWait("input2")
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
善良天后 2024-12-17 04:03:36

您只需在 powershell 中使用 Expect 程序即可。有用。 Powershell也是一个shell,你可以运行powershell编写的代码,它调用bash代码,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
}

让我解释一下测试。

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

You just use Expect program in your powershell. It works. Powershell is a shell too, you can run code wrote by powershell, which call bash code, which call powershell again.

Bellow is a test, it passed.

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
}

Let me explain the test.

  1. create a bash file which expect an input.
  2. create a tcl file which call bash created in step one.
  3. invoke tcl program from powershell, it works, will not waiting for input.
饭团 2024-12-17 04:03:36

我不知道有任何本地能力可以精确复制。这个问题有一个答案,声称能够将内容传递给进程/从进程传递内容,因此它可能符合您的需求。

如何在另一个应用程序窗口中运行交互式命令powershell

祝你好运!

I am not aware of any native capability to duplicate exact. This question has an answer that claims to be able to pass content to/from a process, so it might work with what you want.

How to run interactive commands in another application window from powershell

Good Luck!

信仰 2024-12-17 04:03:36

Lee Holmes 于 2014 年在 Powershell Gallery 上发表了一篇名为“Await”的“Expect for Powershell”。事实证明,模拟 Expect 比您想象的要复杂得多,其中涉及 Win32 调用。

套餐
https://www.powershellgallery.com/packages/Await/0.8

演示
https://www.youtube.com/watch?v=tKyAVm7bXcQ

Lee Holmes put out an "Expect for Powershell" in 2014 on the Powershell Gallery called Await. Turns out emulating expect is a lot more complicated than you'd imagine, involving the Win32 calls.

Package
https://www.powershellgallery.com/packages/Await/0.8

Demo
https://www.youtube.com/watch?v=tKyAVm7bXcQ

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文