10

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

lander16
  • 921
  • 1
  • 9
  • 12

5 Answers5

2

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"`
A Jain
  • 41
  • 7
1

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.
Jiang Libo
  • 21
  • 1
  • 3
1

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}")
Vijred
  • 349
  • 1
  • 2
  • 10
0

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!

Community
  • 1
  • 1
Neossian
  • 695
  • 4
  • 14
0

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

RiverHeart
  • 549
  • 6
  • 15