azureazure-clijmespath

Bash variable in JMESPath query expression


I have such a code:

# Extract zones
$Zones = az network private-dns zone list -g "rg-test-westeurope" --query '[].{Name: name}' --output json | ConvertFrom-Json

# Initialize an empty array to store record sets
$RecordSets = @()

# Iterate over each zone
foreach ($zone in $Zones) {
     $zone.Name
    # List record sets for the current zone and append to the array
    $RecordSets += az network private-dns record-set a list -g "rg-test-westeurope" -z $zone.Name --query '[].{Zone:$zone.Name, IPv4: aRecords[0].ipv4Address, Name: name}' --output json | ConvertFrom-Json
}

but this part doesnt work for me: Zone:$zone.Name How can I integrate $zone.Name in my query to have such result

# Convert the array of record sets into a table
$RecordSets | Format-Table -Property Zone, IPv4, Name

Solution

  • Initially, I too got similar response when I ran this PowerShell script to list record sets with their DNS zone names:

    # Extract zones
    $Zones = az network private-dns zone list -g "rgName" --query '[].{Name: name}' --output json | ConvertFrom-Json
    
    # Initialize an empty array to store record sets
    $RecordSets = @()
    
    # Iterate over each zone
    foreach ($zone in $Zones) {
         $zone.Name
        # List record sets for the current zone and append to the array
        $RecordSets += az network private-dns record-set a list -g "rgName" -z $zone.Name --query "[].{Zone:'$zone.Name', IPv4: aRecords[0].ipv4Address, Name: name}" --output json | ConvertFrom-Json
    }
    
    # Convert the array of record sets into a table
    $RecordSets | Format-Table -Property Zone, IPv4, Name
    

    Response:

    enter image description here

    To get the desired output, you can make use of below modified script where I got the response successfully like this:

    # Extract zones
    $Zones = az network private-dns zone list -g "rgName" --query '[].{Name: name}' --output json | ConvertFrom-Json
    
    # Initialize an empty array to store record sets
    $RecordSets = @()
    
    # Iterate over each zone
    foreach ($zone in $Zones) {
        # Extract zone name
        $zoneName = $zone.Name
    
        # List record sets for the current zone and append to the array
        $RecordSets += az network private-dns record-set a list -g "rgName" -z $zoneName --query "[].{Zone: '$zoneName', IPv4: aRecords[0].ipv4Address, Name: name}" --output json | ConvertFrom-Json
    }
    
    # Convert the array of record sets into a table
    $RecordSets | Format-Table -Property Zone, IPv4, Name
    

    Response:

    enter image description here