使用 powershell 自动批处理文件提示
Automate batch file prompts with powershell
我有一个批处理文件,它会提示用户几次。我希望使用 powershell 实现自动化。有什么办法吗?我需要这样的东西:
Start-Process $InstallDir\Install.bat "y,*,$Version,y,y,y,y,y,y,y,y,y,y,y,y,y"
Install.bat运行一次安装,一共有16个提示。第三个我希望它是我的 powershell 脚本中已有的一个变量,但其他变量将是静态的。另外,在脚本结束时,您需要按任意键才能继续。
有什么办法吗?
Read-Host 将显示输入提示,将其分配给一个变量意味着您稍后可以在脚本中使用该条目。
由于您的示例不具体,下面只会让您了解您需要做什么。
$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"
根据您的批处理文件和实际执行提示的命令,您可以使用 input redirection <
。将提示逐行放入文本文件中,然后将其重定向到批处理文件中。
假设批处理文件 prompts.bat
包含以下命令...:[=22=]
@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
...并且文本文件 prompts.txt
包含以下行...:
hello world
Y
n
End
...命令行 prompts.bat < prompts.txt
的控制台输出将是:
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 . . .
(del
命令在此处显示两个提示,因为它收到 Y
后面的 RETURN,choice
; 由于不接受空条目,提示再次出现。)
我有一个批处理文件,它会提示用户几次。我希望使用 powershell 实现自动化。有什么办法吗?我需要这样的东西:
Start-Process $InstallDir\Install.bat "y,*,$Version,y,y,y,y,y,y,y,y,y,y,y,y,y"
Install.bat运行一次安装,一共有16个提示。第三个我希望它是我的 powershell 脚本中已有的一个变量,但其他变量将是静态的。另外,在脚本结束时,您需要按任意键才能继续。
有什么办法吗?
Read-Host 将显示输入提示,将其分配给一个变量意味着您稍后可以在脚本中使用该条目。
由于您的示例不具体,下面只会让您了解您需要做什么。
$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"
根据您的批处理文件和实际执行提示的命令,您可以使用 input redirection <
。将提示逐行放入文本文件中,然后将其重定向到批处理文件中。
假设批处理文件 prompts.bat
包含以下命令...:[=22=]
@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
...并且文本文件 prompts.txt
包含以下行...:
hello world
Y
n
End
...命令行 prompts.bat < prompts.txt
的控制台输出将是:
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 . . .
(del
命令在此处显示两个提示,因为它收到 Y
后面的 RETURN,choice
; 由于不接受空条目,提示再次出现。)