
function main {
$options = @('option1', 'option2', 'option3')
CLS; $n = 1
$options | foreach { "[{0}] {1}" -f $n, $_; $n++ }
choice /c 123 /m "Choose:"
"opt$lastexitcode"
pause; main
}
function opt1 { "this is option one" }
function opt2 { "this is option two" }
function opt3 { "this is option three" }
main
如果可能的話,透過鍵入函數名稱直接呼叫其他函數,而不需要如果或者轉變陳述?
答案1
你也許可以在這裡使用 呼叫表達式命令。
文章包含以下範例:
$Command = "Get-Process"
Invoke-Expression $Command
請記住以下文章中的警告(可能與您的情況無關):
呼叫表達式被認為是有害的。