我有这个简短的脚本,它显示一个带有行号的表格,并询问用户想要使用哪个 Azure 订阅。它工作得很好。
$subscriptions = $(& az account list --query '[].{name:name}' --output json) | ConvertFrom-Json
$subscriptions | ForEach-Object {$index=0} {$_; $index++} | Format-Table -Property @{ Label="index";Expression={$index}; Width=5 },Name
$subChoice = Read-Host 'Choose subscription'
现在我想编写一个小函数来显示表格并从中选择一个项目,以便将其重用于其他选择。
function GetChoice {
param (
[Parameter(Mandatory = $true)][psobject] $list,
[Parameter(Mandatory = $true)][string] $prompt
)
$list | ForEach-Object {$index=0} {$_; $index++} | Format-Table -Property @{ Label="index";Expression={$index}; Width=5 },Name
$choice = Read-Host $prompt
}
当我用它调用它时,$subChoice = GetChoice $subscriptions 'Choose subscription'
它不会显示表格。
为什么它不起作用,我应该修改什么才能使它起作用?