title: How to loop through list of objects in terraform?
body: <p>I am trying to dynamically fetch list of VMs from azure using external data source and display VM individually.</p>
  
  <p>Below is powershell script</p>
  
  <pre><code>$rgroup = [Console]::In.ReadLine()
  $json = ConvertFrom-Json $rgroup
  $name = $json.rg
  $vm=Get-AzVM -ResourceGroupName $name  | select name  |  convertTo-json
  Write-Output  "$vm"
  </code></pre>
  
  <p>Main.tf</p>
  
  <pre><code>variable "resourcegroup" {}
  data "external" "test" {
  program = ["Powershell.exe", "./vm.ps1"]
  query = {
  rg = "${var.resourcegroup}"
  }}
  output "value" {
  value = "${data.external.test.result}"}
  </code></pre>
  
  <p>However, I am getting an error " command "Powershell.exe" produced invalid JSON: json: cannot unmarshal number into Go value of type map[string]string"</p>
  
  <p>Can someone tel me how to loop through list of VMs and display it individually ? </p>
  
  <p>-------------Edited------------</p>
  
  <p>Powershell Script </p>
  
  <pre><code>$rgroup = [Console]::In.ReadLine()
  $json = ConvertFrom-Json $rgroup
  $name = $json.rg
  $vms=(Get-AzVM -ResourceGroupName $name ).name
  foreach ($vm in $vms){
  $vmname= $vm |convertTo-json
  Write-Output  "{""Name"" : $vmname}"}
  </code></pre>
  
  <p>Main.tf</p>
  
  <pre><code>output "value" {
  value = "${data.external.powershell_test.result.Name}"}
  </code></pre>
  
  <p>Powershell output</p>
  
  <p><a href="https://i.stack.imgur.com/S1XBK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/S1XBK.png" alt="enter image description here"></a></p>