How to make a Powershell script answer a prompt for user input?
Asked Answered
G

2

16

I have a Powershell script that needs to execute a command-line executable that prompts for user input (for which there is no command-line parameter). Something like this:

# promptsForInput.ps1
$someInput = Read-Host "Gimme some input"
# do something with $someInput

The actual executable is a compiled binary which I cannot modify, but this example script do for the purposes of this question. My Powershell script calls the executable:

# myScript.ps1
.\promptsForInput.ps1

At runtime, I get prompted to input something:

> .\myScript.ps1
Gimme some input:

I need to be able to fill in that input request without user intervention. When promptsForInput.ps1 prompts for user input, I need myScript.ps1 to enter the required value and proceed with script execution.

Similar questions on SO either have answers specific to the particular exe, or are otherwise unhelpful:

Gard answered 19/10, 2017 at 20:51 Comment(0)
A
8
  1. Don't use $input as the name of a variable as that's an automatic variable in PowerShell (see help about_Automatic_Variables).

  2. Determine whether the executable can accept command-line arguments instead of stopping or prompting for input. This is obviously preferred if it is available.

  3. Whether you can tell PowerShell to automatically send input to an arbitrary executable depends upon the design of that executable. If the executable allows you to send standard input to it, you can run something like

    "test" | myexecutable.exe
    

    In this example, PowerShell will provide the string test and a return as standard input to the executable. If the executable accepts standard input, then this will work.

    If the executable does not use standard input, then you will have to do something different like send keystrokes to the window. I consider sending keystrokes a last resort and a brittle solution that is not automation-friendly.

Australasia answered 19/10, 2017 at 21:6 Comment(3)
$input was just used for the example (I'll change it); like I said, the actual program being called is a binary.Gard
Is there a way to extend the pipe method to more inputs? Assuming promptsForInput.ps1 asks for multiple inputs, "foo" | "bar" | .\promptsForInput.ps1 (obviously) won't work, is there a syntax that will?Gard
If you have multiple lines of input, you can separate with `r`n; e.g. "Line 1`r`nLine2". But again: This is entirely dependent on whether the executable accepts standard input. This is not under PowerShell's control.Australasia
B
4

Following sample I used to run batch file and provide inputs

[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}")
Bergson answered 8/10, 2021 at 17:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.