gpt4 book ai didi

arrays - 如何使用数组作为参数?

转载 作者:行者123 更新时间:2023-12-02 23:56:03 24 4
gpt4 key购买 nike

我想提示以编程方式生成的列表中的选项。

背景:
我有2个包含不同环境的AWS账户。
该脚本会自动检测您所在的帐户,然后提示您要加入哪个环境。

我正在尝试:

$envs = "
1,Dev
1,Test
1,Demo
2,Staging
2,Production
" | ConvertFrom-Csv -Delimiter "," -header "awsAccount","Environment"

$awsAccount = Determine-awsAccount

$envs = ([string]($allServers.Environment | Where-Object -property awsAccount -eq $awsAccount | Sort-Object | Get-unique)).replace(" ",",")

$title = "Deploy into which environment"
$message = "Please select which environment you want to deploy into"
$options = [System.Management.Automation.Host.ChoiceDescription[]]($envs)
$result = $host.ui.PromptForChoice($title, $message, $options, 0)

您可以使用以下选项创建弹出菜单 $options = [System.Management.Automation.Host.ChoiceDescription[]]("yes","no")
但就我而言,它弹出一个包含我所有环境的选项,以逗号分隔。我希望它为每个(相关)环境弹出一个选项。

如何将环境的字符串列表从PowerShell内环境弹出到PowerShell外部环境?

最佳答案

我将您的问题读为:

When awsAccount 1 is relevant, give the options for awsAccount 1 (Dev,test, Demo)"

When awsAccount 2 is relevant, give the options for awsAccount 2 (Demo,Staging, Production)"


主要更改是您的 $envs = ([string](..行。我使用了一个新的 $envsToDisplayInPrompt变量,以避免与原始 $envs混淆。
代码:
$envs = "
1,Dev
1,Test
1,Demo
2,Staging
2,Production
" | ConvertFrom-Csv -Delimiter "," -header "awsAccount","Environment"

#$awsAccount = Determine-awsAccount
$awsAccount = 1 # assuming Determine-awsAccount returns an integer 1 or 2

#$envs = ([string]($allServers.Environment | Where-Object -property awsAccount -eq $awsAccount | Sort-Object | Get-unique)).replace(" ",",")
$envsToDisplayInPrompt = @(($envs | Where-Object {$_.awsAccount -eq $awsAccount}).Environment)

$title = "Deploy into which environment"
$message = "Please select which environment you want to deploy into"
$options = [System.Management.Automation.Host.ChoiceDescription[]]($envsToDisplayInPrompt)
$result = $host.ui.PromptForChoice($title, $message, $options, 0)
输出:
Prompt output

关于arrays - 如何使用数组作为参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44760904/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com