user1911612 user1911612 - 3 months ago 9
PowerShell Question

Automate batch file prompts with powershell

I have a batch file which prompts the user a few times. I am looking to automate that with powershell. Is there any way to do this? I would need something like this:

Start-Process $InstallDir\Install.bat "y,*,$Version,y,y,y,y,y,y,y,y,y,y,y,y,y"


Install.bat runs an installation and there are a total of 16 prompts. The third I would like the be a variable that I have in my powershell script already, but the others will be static. Also, at the end of the script, you need to press any key to continue.

Is there any way to do this?

Answer

Depending on your batch file and what commands actually do the prompt, you might use input redirection <. Put the prompts into a text file pine by line and redirect that into your batch file.

Supposing the batch file prompts.bat contains the following commands...:

@echo off
set /P VAR="Please enter some text: "
echo/
echo Thank you for entering "%VAR%"!
choice /M "Do you want to continue "
if not ErrorLevel 2 del "%TEMP%\*.*"
pause

...and the text file prompts.txt contains the following lines...:

hello world
Y
n
End

...the console output of the command line prompts.bat < prompts.txt would be:

Please enter some text:
Thank you for entering "hello world"!
Do you want to continue [Y,N]?Y
C:\Users\operator\AppData\Local\Temp\*.*, Are you sure (Y/N)?
C:\Users\operator\AppData\Local\Temp\*.*, Are you sure (Y/N)? n
Press any key to continue . . .

(The del command shows two prompts here as it receives the RETURN behind Y which is not consumed by choice; since an empty entry is not accepted, the promt appears one more time.)