I have the following problem.
I want to offer the user of my powershell script the choice of what Azure Subscription to use. I've been following this example (included because it also shows the part I can't figure out).
Example
$title = "Delete Files"
$message = "Do you want to delete the remaining files in the folder?"
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
"Deletes all the files in the folder."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
"Retains all the files in the folder."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $host.ui.PromptForChoice($title, $message, $options, 0)
I can get as far as offering the list with the following
My version
After connecting.....
$title = "Azure subscriptions"
$message = "Please pick a subscription"
$all_subscriptions = Get-AzureRmSubscription
$options = [System.Management.Automation.Host.ChoiceDescription[]]($all_subscriptions.name)
$result = $host.ui.PromptForChoice($title, $message, $options, 0)
I'm aware this is missing the part of the code that specifies the choice which in the example is this
New-Object system.Management.Automation.Host.ChoiceDescription "&Yes", `
"Deletes all the files in the folder."`
I've tried a foreach loop using the $all_subscriptions.name
but this (at least how I done this) doesn't seem to work. Can anyone point me in the right direction here. Is PromptForChoice
even the right way to do this?
TLDR;
How do I build a dynamic list a user can select from using PromptForChoice
within powershell
Assuming you have less than 10 choices to pick from, you can prepend &N
and generate the choice descriptions on the fly with hot keys then numbered 1 - 9
:
$choiceIndex = 1
$options = $all_subscriptions |ForEach-Object {
New-Object System.Management.Automation.Host.ChoiceDescription "&$($choiceIndex++) - $($_.Name)"
}
$chosenIndex = $host.ui.PromptForChoice($title, $message, $options, 0)
$SubscriptionToUse = $all_subscriptions[$chosenIndex]