2017-06-26 7 views
2

プログラムで生成されたリストからオプションを要求する必要があります。powershellは配列としてパラメータを使用します

背景: 私は異なる環境を含む2つのAWSアカウントを持っています。 スクリプトは自動的に現在のアカウントを検出し、参加する環境を入力するように要求する必要があります。

私はこれをしようとしている:あなたは $options = [System.Management.Automation.Host.ChoiceDescription[]]("yes","no")

を使用してオプションのポップアップを作成することができます

$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) 

をしかし、私の場合には、それは、カンマで区切られた、すべての私の環境を含む一つの選択肢をポップアップです。それぞれの(関連する)環境ごとに1つのオプションをポップアップしたい。

環境の文字列リストをPowerShellの世界から外部のPowerShellの世界に戻すにはどうすればよいですか?

答えて

1

私はあなたの質問を読んでいます:

awsAccount 1が関連している場合は、awsAccount 1(DEV、 テスト、デモ)」のオプションを与える

awsAccount 2が関連している場合には、与えますawsAccount 2(デモ、 ステージング、プロダクション)のオプション」

主な変更は、あなたの$envs = ([string](..ラインにあります。元の$envsとの混乱を避けるため、新しい変数$envsToDisplayInPromptを使用しました。

コード:

$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

+0

素晴らしいです。異なる種類の変数を使用した主な変更点はありますか?私はそれが動作することがわかりますが、どのように見ることはできません。 –

+0

条件を$ _。awsAccount-like "*" + $ awsAccount + "*"に変更して動作させました。 –

+0

@RichardMooreここまでうれしいです。主な変更点は '$ envs =([string](..') '$ options = ...'で必要とされる文字列配列を正しく返すとは確信していないからです。 -Host $ env' _after_この行を見て、それが何を表示しているのかを見てください。同じ変数名を使うのは普通ですが、わかりやすくするために別のものを使っています。 – gms0ulman

関連する問題