Why is the hash table output a single string not a array?
$ServicesList = @{
"Exhange Online" = "Connect-ExchangeOnline"
"Security & Compliance Center" = "Connect-IPPSSession"
"Azure Active Directory" = "Connect-AzureAD"
"Microsoft Online" = "Connect-MsolService"
"SharePoint Online" = "Connect-SPOService -Url $url"
"Microsoft Teams" = "Connect-MicrosoftTeams"
"Microsoft Graph" = "Connect-Graph"
"Azure Account" = "Connect-AzAccount"
}
$ServicesListKeys = $ServicesList.Keys
function Menu {
$menu = @{}
for ($i = 1; $i -le $ServicesListKeys.count; $i++) {
Write-Host "$i. $($ServicesListKeys[$i-1])"
$menu.Add($i, ($ServicesListKeys[$i - 1]))
}
[int]$ans = Read-Host 'Enter selection'
$selection = $menu.Item($ans) ; $ServicesList[$selection]
}
Menu
Output:
- Azure Active Directory Microsoft Graph Microsoft Online Azure Account Security & Compliance Center Exhange Online Microsoft Teams SharePoint Online
when it workers with a normal array
$array = "Red", "green", "Blue"
Output:
- Red
- green
- Blue
$ServicesListKeysis not indexable, so PowerShell returns the object itself for$ServicesListKeys[0], and `````$null``` for all other indices. Compare to(1`)[0](returns1) vs(1)[1](returns$null). You can coerce it into an array with$ServicesListKeys =@( $ServicesList.Keys )and then your remaining code will work fine...