【问题标题】:How to request user input from the command line (at the same time that you run the script from the command line)如何从命令行请求用户输入(在从命令行运行脚本的同时)
【发布时间】:2019-10-15 19:48:19
【问题描述】:

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

例如:

C:\User\Desktop\Script.ps1 -input hello

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

$D = Read-Host -Prompt "Input here"

【问题讨论】:

  • C:\User\Desktop\Script.ps1 -input (Read-Host "Input Here) ?您应该使用除input 之外的其他参数名称,因为这将导致您的脚本中出现$input$input 是保留变量。
  • 知道了,我可以改一下。

标签: xcode powershell command-line


【解决方案1】:

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

在命令行提示用户:

Script.ps1 内容:

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

运行脚本:

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

输出:

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

输出:

Input Here: hello
the input was hello

【讨论】:

  • 谢谢!这帮助很大!
猜你喜欢
  • 2017-12-07
  • 2019-06-11
  • 1970-01-01
  • 1970-01-01
  • 2018-10-05
  • 2020-03-15
  • 2012-05-03
  • 2011-05-01
相关资源
最近更新 更多