繁体   English   中英

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

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

我有一个名为 Script.ps1 的 PowerShell 脚本。 该脚本有一个读取主机输入提示,但我想在运行脚本之前进行输入。

例如:

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

这是请求输入的脚本部分:

$D = 读取主机 - 提示“在此处输入”

您可以创建一个脚本文件,在发生参数绑定时或在命令行中提示用户。

在命令行提示用户:

Script.ps1 内容:

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

运行脚本:

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

Output:

input here: Hello
the input was Hello

在参数绑定期间提示用户:

Script.ps1 内容:

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

执行脚本:

C:\User\Desktop\Script.ps1

Output:

Input Here: hello
the input was hello

暂无
暂无

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

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