Equivalent to bash "expect" in powershell

I use powershell to run another powershell script, at some point another script asks for some input, I would like to be able to read the output from another script and based on this sentence input to it. Similar to what you can do with waiting on bash.

Any ideas?

thanks

+6
source share
3 answers

I do not know about any own ability to duplicate exact. This question has an answer that claims to be able to transfer content to / from the process, so it can work with what you want.

How to run interactive commands in another application window from powershell

Good luck

0
source

You just use Expect in your PowerShell. It is working. Powershell is also a shell; you can run code written by powershell that calls bash code that calls 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.

  • create a bash file that is waiting for input.
  • create a tcl file that calls the bash created in the first step.
  • call the tcl program from powershell, it works, will not wait for input.
0
source

Just submit my solution so that it can help someone. I ran into the same problem when running some other scripts that would request responses. First, create an inputFileLocation.txt file with the answers to each question on each line in the sequence. Then run the script in the syntax below. And he will do the job.

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

Source: https://habr.com/ru/post/899498/


All Articles