PowerShell 提示选择
PowerShell PromptForChoice
我有以下问题。
我想为我的 powershell 脚本的用户提供使用 Azure 订阅的选择。我一直在关注这个例子(包括在内是因为它也显示了我无法弄清楚的部分)。
例子
$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)
我可以提供以下列表
我的版本
连接后......
$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)
我知道这缺少指定示例中的选择的代码部分
New-Object system.Management.Automation.Host.ChoiceDescription "&Yes", `
"Deletes all the files in the folder."`
我试过使用 $all_subscriptions.name
的 foreach 循环,但这(至少我是怎么做的)似乎不起作用。谁能在这里指出我正确的方向。 PromptForChoice
这样做是否正确?
TLDR;
如何构建用户可以 select 通过在 powershellPromptForChoice
中使用
的动态列表
假设可供选择的选项少于 10 个,您可以在前面添加 &N
并使用热键即时生成选项描述,然后编号为 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]
看来您可以只传递一个字符串数组作为选项。
[string[]]$choices = "machine", "another machine", "the third one", "obviously this one!"
$result = $host.ui.PromptForChoice($title, $message, $choices, 0)
Write-Host $choices[$result] -ForegroundColor Green
$result
是一个整数,表示选择的索引。然后我们可以返回我们的数组并在那个位置抓取项目 hey-presto - 你得到你的结果!
我有以下问题。
我想为我的 powershell 脚本的用户提供使用 Azure 订阅的选择。我一直在关注这个例子(包括在内是因为它也显示了我无法弄清楚的部分)。
例子
$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)
我可以提供以下列表
我的版本
连接后......
$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)
我知道这缺少指定示例中的选择的代码部分
New-Object system.Management.Automation.Host.ChoiceDescription "&Yes", `
"Deletes all the files in the folder."`
我试过使用 $all_subscriptions.name
的 foreach 循环,但这(至少我是怎么做的)似乎不起作用。谁能在这里指出我正确的方向。 PromptForChoice
这样做是否正确?
TLDR;
如何构建用户可以 select 通过在 powershellPromptForChoice
中使用
假设可供选择的选项少于 10 个,您可以在前面添加 &N
并使用热键即时生成选项描述,然后编号为 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]
看来您可以只传递一个字符串数组作为选项。
[string[]]$choices = "machine", "another machine", "the third one", "obviously this one!"
$result = $host.ui.PromptForChoice($title, $message, $choices, 0)
Write-Host $choices[$result] -ForegroundColor Green
$result
是一个整数,表示选择的索引。然后我们可以返回我们的数组并在那个位置抓取项目 hey-presto - 你得到你的结果!