用于获取 VM 名称和专用 IP 地址的 Azure 图形查询
Azure graph query to fetch VM Name and Private IP address
我正在尝试根据标签过滤实例。我正在使用以下命令列出具有 wknhscale == 'active' 标记的实例。它工作正常,returns 实例名称和资源组。
az graph query -q "Resources | where type =~ 'Microsoft.Compute/virtualMachines'| where tags['wknhscale']=='active' | project name, resourceGroup"| jq '[.data[] | {name, resourceGroup}]'
现在同时,我也想获取实例的IP地址。所以我正在使用下面的查询,但它没有给我任何数据。
az graph query -q "Resources | where type =~ 'Microsoft.Compute/virtualMachines' type =~ 'Microsoft.Compute/privateIPAddresses'| where tags['wknhscale']=='active'"
以下是我如何使用 KQL 查询检索虚拟机实例名称和资源组。
Resources
| where type =~ 'microsoft.compute/virtualmachines'
| project vmId = tolower(tostring(id)), vmName = name
| join (Resources
| where type =~ 'microsoft.network/networkinterfaces'
| mv-expand ipconfig=properties.ipConfigurations
| project vmId = tolower(tostring(properties.virtualMachine.id)), privateIp = ipconfig.properties.privateIPAddress, publicIpId = tostring(ipconfig.properties.publicIPAddress.id)
| join kind=leftouter (Resources
| where type =~ 'microsoft.network/publicipaddresses'
| project publicIpId = id, publicIp = properties.ipAddress
) on publicIpId
| project-away publicIpId, publicIpId1
| summarize privateIps = make_list(privateIp), publicIps = make_list(publicIp) by vmId
) on vmId
| project-away vmId1
| sort by vmName asc
| where array_length(publicIps)>0
我可以使用以下查询检索实例名称和私有地址。
注意:您可能需要基于订阅(--订阅)来了解环境变量或 azure 配置的设置方式。
az vm list-ip-addresses --ids $(az resource list -g test-group --query "[?type=='Microsoft.Compute/virtualMachines' && tags.wknhscale== 'active'].id" -o tsv) --query "[].{Name:virtualMachine.name, RG:virtualMachine.resourceGroup, IP:virtualMachine.network.privateIpAddresses[0]}"
输出
[
{
"IP": "11.190.0.42",
"Name": "hscalenode04",
"RG": "test-group"
},
{
"IP": "11.190.0.43",
"Name": "hscalenode03",
"RG": "test-group"
},
{
"IP": "11.190.0.44",
"Name": "hscalenode05",
"RG": "test-group"
},
{
"IP": "11.190.0.45",
"Name": "hscalenode02",
"RG": "test-group"
},
{
"IP": "11.190.0.46",
"Name": "hscalenode01",
"RG": "test-group"
}
]
我正在尝试根据标签过滤实例。我正在使用以下命令列出具有 wknhscale == 'active' 标记的实例。它工作正常,returns 实例名称和资源组。
az graph query -q "Resources | where type =~ 'Microsoft.Compute/virtualMachines'| where tags['wknhscale']=='active' | project name, resourceGroup"| jq '[.data[] | {name, resourceGroup}]'
现在同时,我也想获取实例的IP地址。所以我正在使用下面的查询,但它没有给我任何数据。
az graph query -q "Resources | where type =~ 'Microsoft.Compute/virtualMachines' type =~ 'Microsoft.Compute/privateIPAddresses'| where tags['wknhscale']=='active'"
以下是我如何使用 KQL 查询检索虚拟机实例名称和资源组。
Resources
| where type =~ 'microsoft.compute/virtualmachines'
| project vmId = tolower(tostring(id)), vmName = name
| join (Resources
| where type =~ 'microsoft.network/networkinterfaces'
| mv-expand ipconfig=properties.ipConfigurations
| project vmId = tolower(tostring(properties.virtualMachine.id)), privateIp = ipconfig.properties.privateIPAddress, publicIpId = tostring(ipconfig.properties.publicIPAddress.id)
| join kind=leftouter (Resources
| where type =~ 'microsoft.network/publicipaddresses'
| project publicIpId = id, publicIp = properties.ipAddress
) on publicIpId
| project-away publicIpId, publicIpId1
| summarize privateIps = make_list(privateIp), publicIps = make_list(publicIp) by vmId
) on vmId
| project-away vmId1
| sort by vmName asc
| where array_length(publicIps)>0
我可以使用以下查询检索实例名称和私有地址。
注意:您可能需要基于订阅(--订阅)来了解环境变量或 azure 配置的设置方式。
az vm list-ip-addresses --ids $(az resource list -g test-group --query "[?type=='Microsoft.Compute/virtualMachines' && tags.wknhscale== 'active'].id" -o tsv) --query "[].{Name:virtualMachine.name, RG:virtualMachine.resourceGroup, IP:virtualMachine.network.privateIpAddresses[0]}"
输出
[
{
"IP": "11.190.0.42",
"Name": "hscalenode04",
"RG": "test-group"
},
{
"IP": "11.190.0.43",
"Name": "hscalenode03",
"RG": "test-group"
},
{
"IP": "11.190.0.44",
"Name": "hscalenode05",
"RG": "test-group"
},
{
"IP": "11.190.0.45",
"Name": "hscalenode02",
"RG": "test-group"
},
{
"IP": "11.190.0.46",
"Name": "hscalenode01",
"RG": "test-group"
}
]