bulkmoustache
bulkmoustache

Reputation: 2025

Print all VM names and private IP from subnet

I want to list all Virtual Machine names that contain a private IP address under a specific subnet (e.g., named "sub-a"). How do I do that?

I was hoping that this query in Azure Resource Graph Explorer would at least print all non empty private IP addresses:

Resources
| where type =~ 'microsoft.compute/virtualmachines' and isnotempty(properties.privateIPAddress)

Upvotes: 0

Views: 1991

Answers (1)

Ken W - Zero Networks
Ken W - Zero Networks

Reputation: 3804

You need to look at the Network Interfaces and expand the properties to pull the Private IP address. Something like this should do the trick. I modified one of our examples to pull private IP instead of public IP.

 Resources
| where type =~ 'microsoft.compute/virtualmachines'
| extend nics=array_length(properties.networkProfile.networkInterfaces)
| mv-expand nic=properties.networkProfile.networkInterfaces
| where nics == 1 or nic.properties.primary =~ 'true' or isempty(nic)
| project vmId = id, vmName = name, vmSize=tostring(properties.hardwareProfile.vmSize), nicId = tostring(nic.id)
| join kind=leftouter (
    Resources
    | where type =~ 'microsoft.network/networkinterfaces'
    | extend ipConfigsCount=array_length(properties.ipConfigurations)
    | extend subnet = tostring(properties.ipConfigurations[0].properties.subnet) 
    | mv-expand ipconfig=properties.ipConfigurations
    | where ipConfigsCount == 1 or ipconfig.properties.primary =~ 'true'
    | project nicId = id, subnet, privateIp = tostring(ipconfig.properties.privateIPAddress))
on nicId
    | order by subnet asc

Upvotes: 2

Related Questions