I have this short script which displays a table with row numbers and asks the user which Azure subscription wants to use. It works neatly.
$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'
Now I want to write a little function for displaying the table and choosing an item from it, to reuse it for other choices.
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
}
When I'm calling it with $subChoice = GetChoice $subscriptions 'Choose subscription' it doesn't display the table.
Why it doesn't work and what should I modify in order to make it work?