powershell使用数组作为参数

时间:2017-06-26 13:28:18

标签: arrays powershell prompt pop

我想从程序生成的列表中提示选项。

背景: 我有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世界?

1 个答案:

答案 0 :(得分:1)

我正在阅读你的问题:

  

当awsAccount 1相关时,请为awsAccount 1(Dev,   测试,演示)"

     

当awsAccount 2相关时,请为awsAccount 2提供选项(Demo,   分期,制作)"

主要更改是$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