JMESPath 查询表达式中的 Bash 变量

问题描述 投票:0回答:1

我有这样的代码:

# 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
}

但这部分对我不起作用:

Zone:$zone.Name
如何在我的查询中集成 $zone.Name 以获得这样的结果

# Convert the array of record sets into a table
$RecordSets | Format-Table -Property Zone, IPv4, Name
azure azure-cli jmespath
1个回答
0
投票

最初,当我运行此 PowerShell 脚本来列出记录集及其 DNS 区域名称时,我也得到了 类似的响应

# 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

回复:

enter image description here

要获得所需的输出,您可以使用下面的修改过的脚本,我在其中成功获得了响应,如下所示:

# 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

回复:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.