Export Azure VM Info
Hello,
I would like to export list of virtual machines & their corresponding: Name, IP, Type (Windows or Linux for example), CPU, RAM, HDD\SSD and Tag
As I know it is possible with Azure Resource Graph, but when I tried applied filters I cant see anything... How do I should write script correct?
Thanks in advance.
Azure Virtual Machines
-
Irakli 0 Reputation points
2024-10-02T12:31:33.96+00:00 Resources
| where type == 'microsoft.compute/virtualmachines'
| extend
OSName = tostring(properties.osProfile.computerName),
OSType = tostring(properties.storageProfile.osDisk.osType),
VMSize = tostring(properties.hardwareProfile.vmSize)
| join kind=leftouter
( Resources
| where type =~ 'microsoft.network/networkinterfaces'
not sure what is the next and how to select cpu\ram...
-
Mahesh Goud Juvvadi 1,060 Reputation points • Microsoft Vendor
2024-10-03T05:47:46.82+00:00 Hi Irakli,
Thank you for reaching out to the Microsoft Q&A platform.
Yes, use the Azure Resource Graph Explorer to export VM configuration data, including CPU, RAM, OS disk size.
The Azure Resource Graph Explorer allows you to run Kusto Query Language (KQL) queries against your Azure resources and export the results to a CSV file.To export VM configuration data using the Azure Resource Graph Explorer, you can follow these steps:
- Open the Azure Resource Graph Explorer in the Azure portal.
- Write a KQL query to retrieve the VM configuration data. Here's an example query:
Resources | where type == 'microsoft.compute/virtualmachines' | extend vmSize = tostring(properties.hardwareProfile.vmSize) | extend osDiskSize = tostring(properties.storageProfile.osDisk.diskSizeGB) | extend dataDiskSize = tostring(properties.storageProfile.dataDisks[0].diskSizeGB) | project name, vmSize, osDiskSize, dataDiskSize
This query retrieves all virtual machines in your subscription and projects the name, VM size, OS disk size.
- Run the query and export the results to a CSV file. To export the results to a CSV file, click the "Export" button in the Azure Resource Graph Explorer and select "CSV (current view)".
- Open the CSV file in Excel or another spreadsheet program to view and analyze the data. I hope this helps! Let me know if you have any other questions.
Please refer the document for your reference: Azure Resource Graph sample queries for Azure Virtual Machines
If it was helpful, please click "Upvote" on his post to let us know.
Thank You.
-
Irakli 0 Reputation points
2024-10-03T10:22:26.9966667+00:00 Thanks for your reply, how I can also add network settings output?
Do I need "join kind=leftouter"?
-
Irakli 0 Reputation points
2024-10-03T10:53:38.9566667+00:00 I tried in this way but I see errors
resources
| where type == 'microsoft.compute/virtualmachines'
| extend vmSize = tostring(properties.hardwareProfile.vmSize)
| extend vmType = tostring(properties.storageProfile.osDisk.osType)
| extend osDiskSize = tostring(properties.storageProfile.osDisk.diskSizeGB)
| extend dataDiskSize = tostring(properties.storageProfile.dataDisks[0].diskSizeGB)
| project resourceGroup, name, vmType, osDiskSize, dataDiskSize
| join kind=leftouter
resources
| where type =~ 'microsoft.network/networkinterfaces'
| project id, ipConfigurations = properties.ipConfigurations
| mvexpand ipConfigurations
| project id, subnetId = tostring(ipConfigurations.properties.subnet.id)
| parse kind=regex subnetId with '/virtualNetworks/' virtualNetwork '/subnets/' subnet
| project id, virtualNetwork, subnet
-
Mahesh Goud Juvvadi 1,060 Reputation points • Microsoft Vendor
2024-10-04T09:11:36.07+00:00 -
Mahesh Goud Juvvadi 1,060 Reputation points • Microsoft Vendor
2024-10-07T05:30:02.2433333+00:00 Hello IrakliIrakli,
Yes, you would typically use a
join kind=leftouter
to combine the virtual machine data with its associated network interface information. This allows you to retrieve the VM data, including network settings like private IP, virtual network, and subnet details. If you also want to include other network-related settings (such as public IP addresses), you can extend the query to join with the public IP resource type.Here’s an extended query that includes network settings for both private and public IP addresses:
Updated Query Including Network Settings:
Resources | where type == 'microsoft.compute/virtualmachines' | extend vmSize = tostring(properties.hardwareProfile.vmSize) | extend vmType = tostring(properties.storageProfile.osDisk.osType) | extend osDiskSize = tostring(properties.storageProfile.osDisk.diskSizeGB) | extend dataDiskSize = tostring(properties.storageProfile.dataDisks[0].diskSizeGB) | extend nicId = tostring(properties.networkProfile.networkInterfaces[0].id) | project resourceGroup, name, vmType, osDiskSize, dataDiskSize, nicId // Join VM details with Network Interface details | join kind=leftouter ( Resources | where type =~ 'microsoft.network/networkinterfaces' | mvexpand ipConfigurations = properties.ipConfigurations | project nicId = id, privateIpAddress = tostring(ipConfigurations.properties.privateIPAddress), subnetId = tostring(ipConfigurations.properties.subnet.id) | parse kind=regex subnetId with '/virtualNetworks/' virtualNetwork '/subnets/' subnet | project nicId, privateIpAddress, virtualNetwork, subnet ) on nicId // Optional: Join to get public IP details | join kind=leftouter ( Resources | where type =~ 'microsoft.network/publicipaddresses' | project publicIpId = id, publicIpAddress = properties.ipAddress, allocationMethod = properties.publicIPAllocationMethod ) on $left.nicId == $right.publicIpId // Project final result | project resourceGroup, name, vmType, osDiskSize, dataDiskSize, privateIpAddress, publicIpAddress, virtualNetwork, subnet, allocationMethod
If it was helpful, please click "Upvote" on his post to let us know.
Thank You.
Sign in to comment