Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

  • How to fill the prompt in powershell script
  • Powershell: Uninstall Software that has prompts for user input
  • How to "answer" a console input prompt in PowerShell (in the code)
  • How to write to user input in powershell
like image 544
asgallant Avatar asked Oct 19 '17 20:10

asgallant


People also ask

How can I prompt for input for my Windows PowerShell script?

Summary: Learn how to prompt for input for your Windows PowerShell script. How can I use Windows PowerShell as an easy way to prompt a user for information for my script? Use Read-Host, for example: $a = Read-Host -Prompt "enter your input". The users input is stored in the $a variable.

How to request user input using PowerShell?

Start a Powershell command-line. Request a user input using Powershell. Display the user input using Powershell. In our example, the user input was stored in a variable named NAME. Congratulations! You are able to request user input using Powershell.

How to prompt a user for input using a batch script?

Call the function of the Batch script. In our example, we created a function named MYPING. On the notepad application, create a Batch script named TEST. Here is the script result. Congratulations! You are able to prompt a user for input using a Batch script function.

How do I get user input from the command line?

Batch Script - Prompting for user input Start a command-line prompt. Request a user input using the command line. @echo off set /p MYNAME="Name: " Display the user input. echo Your name is: %MYNAME% In our example, the user input was stored in a variable named MYNAME. On the notepad application, create a Batch script named TEST.


1 Answers

  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.

like image 103
Bill_Stewart Avatar answered Oct 31 '22 03:10

Bill_Stewart