我想要的是:執行我的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