简体   繁体   English

如何从命令行请求用户输入(在从命令行运行脚本的同时)

[英]How to request user input from the command line (at the same time that you run the script from the command line)

I have a PowerShell script named Script.ps1.我有一个名为 Script.ps1 的 PowerShell 脚本。 This script has a Read-Host input prompt but I want to make the input BEFORE running the script.该脚本有一个读取主机输入提示,但我想在运行脚本之前进行输入。

For example:例如:

C:\User\Desktop\Script.ps1 -input hello C:\User\Desktop\Script.ps1 -输入你好

This is the part of the script that requests the input:这是请求输入的脚本部分:

$D = Read-Host -Prompt "Input here" $D = 读取主机 - 提示“在此处输入”

You can create a script file that prompts the user when parameter binding happens or at the command line.您可以创建一个脚本文件,在发生参数绑定时或在命令行中提示用户。

Prompting User At Command Line:在命令行提示用户:

Script.ps1 Contents: Script.ps1 内容:

param(
    [string]$in
)
"the input was $in"

Running the Script:运行脚本:

C:\User\Desktop\Script.ps1 -in (Read-Host "Input Here")

Output: Output:

input here: Hello
the input was Hello

Prompting User During Parameter Binding:在参数绑定期间提示用户:

Script.ps1 Contents: Script.ps1 内容:

param(
    [string]$in = (Read-Host "Input Here")
)
"the input was $in"

Executing Script:执行脚本:

C:\User\Desktop\Script.ps1

Output: Output:

Input Here: hello
the input was hello

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM