rjbogz
rjbogz

Reputation: 870

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?

Upvotes: 0

Views: 1596

Answers (2)

aschipfl
aschipfl

Reputation: 34919

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 prompt appears one more time.)

Upvotes: 1

henrycarteruk
henrycarteruk

Reputation: 13227

Read-Host will display a prompt for entry, assigning it to a variable means you can then use that entry later in the script.

As your example is non-specific the below will only give you an idea of what you need to do.

$InstallDir = "C:\folder"
$Version = Read-Host -Prompt "Enter Version Number"
Start-Process "$InstallDir\Install.bat" -ArgumentList "y,*,$Version,y,y,y,y,y,y,y,y,y,y,y,y,y"

Upvotes: 0

Related Questions