0

I am trying to filter instances based on tags. I am using the below command to list instances that have wknhscale == 'active' tag. It is working fine and returns the instance name and resource group.

az graph query -q "Resources | where type =~ 'Microsoft.Compute/virtualMachines'| where tags['wknhscale']=='active' | project name, resourceGroup"| jq '[.data[] | {name, resourceGroup}]'

Now at the same time, I want to fetch the IP address of the Instance also. So I am using the below query, But it's not giving me any data.

az graph query -q "Resources | where type =~ 'Microsoft.Compute/virtualMachines' type =~ 'Microsoft.Compute/privateIPAddresses'| where tags['wknhscale']=='active'"

Eva
  • 515
  • 4
  • 28
  • Does [this question](https://stackoverflow.com/questions/67674782/print-all-vm-names-and-private-ip-from-subnet) answer yours? – jccampanero Dec 28 '21 at 22:36
  • @jccampanero No, it will list the VM's in a particular subnet. I want to query VM which has a specific tag with a private IP – Eva Dec 28 '21 at 23:35

2 Answers2

1

I am able to retrieve the Instances name and Private address using the below query.

Note: You might have to give subscription-based (--subscription) on how your environment variables or azure config is set.

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]}"

Output

[
  {
    "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"
  }
]
Eva
  • 515
  • 4
  • 28
0

Here is how I could able to retrieve virtual machine instance name and resource group using KQL Query.

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

enter image description here

SwethaKandikonda
  • 7,513
  • 2
  • 4
  • 18
  • Thank you very much for your reply! I tried this but weirdly I am not getting consistent results from the query. results are varying for consecutive executions of the command. Also, I tried a couple of examples from [link](https://mihai-albert.com/2020/10/01/get-the-list-of-all-azure-vms-with-all-their-private-and-public-ips/)Mihai-Albert blog and had the same results. So I tried a different approach which was giving consistent results – Eva Dec 30 '21 at 18:46