我想要的是:执行我的powershell脚本(.ps1)并使用答案文件回答所有读取主题问题。Powershell应答文件类似于Linux bash
在Linux上的bash这是简单的看:Answer file Linux bash
我想要的是:执行我的powershell脚本(.ps1)并使用答案文件回答所有读取主题问题。Powershell应答文件类似于Linux bash
在Linux上的bash这是简单的看:Answer file Linux bash
您可以使用子表达式($()
)来调用Read-Host
的情况的参数的说法是不存在的:
param(
$Name = $(Read-Host -Prompt "Input Name"),
$Company = $(Read-Host -Prompt "Input Company Name"),
$Country = $(Read-Host -Prompt "Country")
)
if(-not $Country){
$Country = "US"
}
Write-Host $(@'
Hello, {0} from {1}!
How is the weather in {2}?
'@ -f $Name,$Company,$Country)
# do more stuff
另存为myscript.ps1
。
现在,当你不带任何参数运行它,它会提示您输入的所有值:
PS C:\> .\prompt.ps1
Input Name: Arni
Input Company Name: MegaCorp Inc.
Country:
Hello, Arni from MegaCorp Inc.!
How is the weather in US?
PS C:\>
但是,如果你想在不被提示运行它,只需提供所有必需的参数:
PS C:\> .\prompt.ps1 -Name Xavier -Company "SuperCorp SA" -Country Brazil
Hello, Xavier from SuperCorp SA!
How is the weather in Brazil?
PS C:\>
为什么不修改脚本来读取您的答案文件(如果存在),提示该文件不存在?看起来像一个更优雅的解决方案。 – slashp
该脚本主要通过在命令行上回答读主题问题进行交互式使用。对于测试和重复性任务,选择用答案文件回答问题是非常有用的。 – Arni