Identify resources associated with VM deallocated

Viewed 295

During the cleanup process of Azure VM. I am trying to identify all resources associated with Deallocated VM, Like network, storage and nic.

I ran below query to get details, but unable to write a query to get other details in the same query parameter to get a result of nic, storge in table format.

az vm list -d --query "[?powerState=='VM deallocated']" -o table

qa-automation-10 TEST-QA-AUTOMATION
qa-automation-11 TEST-QA-AUTOMATION
qa-automation-12 TEST-QA-AUTOMATION
qa-automation-13 TEST-QA-AUTOMATION
qa-automation-14 TEST-QA-AUTOMATIO

Any help will be appreciable, I am especially looking for az client query. As VM deallocated list is big so I will run through gitlab pipeline.

},
"id": "/subscriptions/xxxxxxxx/resourceGroups/xxxx/providers/Microsoft.Compute/virtualMachines/x023901",
"identity": null,
"licenseType": null,
"location": "x",
"macAddresses": "",
"name": "x023901",
"networkProfile": {
  "networkInterfaces": [
    {
      "id": "/subscriptions/x/resourceGroups/rGroup-ENV0239/providers/Microsoft.Network/networkInterfaces/x023901nic",
      "primary": null,
      "resourceGroup": "rGroup-ENV0239"
    }
  ]
},
"osProfile": null,
"plan": null,
"powerState": "VM deallocated",
"privateIps": "x.x.x.x",
"provisioningState": "Succeeded",
"publicIps": "",
"resourceGroup": "RGROUP-ENV0239",
"resources": [
  {
    "autoUpgradeMinorVersion": true,
    "forceUpdateTag": null,
    "id": "/subscriptions/xxxxxxx/resourceGroups/RGROUP-ENV0239/providers/Microsoft.Compute/virtualMachines/x023901/extensions/OmsAgentForLinux",
    "instanceView": null,
    "location": "x",
    "name": "OmsAgentForLinux",
    "protectedSettings": null,
    "provisioningState": "Succeeded",
    "publisher": "Microsoft.EnterpriseCloud.Monitoring",
    "resourceGroup": "RGROUP-ENV0239",
    "settings": {
      "stopOnMultipleConnections": true,
      "workspaceId": "xx"
    },
    "tags": null,
    "type": "Microsoft.Compute/virtualMachines/extensions",
    "typeHandlerVersion": "1.0",
    "virtualMachineExtensionType": "OmsAgentForLinux"
  }
],
"storageProfile": {
  "dataDisks": [
    {
      "caching": "None",
      "createOption": "Attach",
      "diskSizeGb": 20,
      "image": null,
      "lun": 0,
      "managedDisk": null,
      "name": "x-data1.vhd",
      "vhd": {
        "uri": "https://x.core.windows.net/vhds/x-data1.vhd"
      },
      "writeAcceleratorEnabled": null
    }
  ],
  "imageReference": null,
  "osDisk": {
    "caching": "ReadWrite",
    "createOption": "Attach",
    "diffDiskSettings": null,
    "diskSizeGb": 30,
    "encryptionSettings": null,
    "image": null,
    "managedDisk": null,
    "name": "xosDisk",
    "osType": "Linux",
    "vhd": {
      "uri": "https://xblob.core.windows.net/vhds/x.vhd"
    },
    "writeAcceleratorEnabled": null
  }
},
"tags": null,
"type": "Microsoft.Compute/virtualMachines",
"vmId": "x",
"zones": null
},
2 Answers

The VMs in Azure has two types, one is the managed VM while the other is unmanaged VM. When you want to show the details of all the VMs, you should pay attention to this.

In addition, there no property about the storage account. You can store the files in the storage account and do not associate to the VM. So you just get a little info about the storage account if the VM is unmanaged.

Get a list of the VM with some resources just like vmName, NIC, osDisk or osDiskURI, the CLI command here:

az vm list --query "[].{VMName:name, nicId:networkProfile.networkInterfaces[0].id, managedDiskId:storageProfile.osDisk.managedDisk.id, UnmanagedDiskURL:storageProfile.osDisk.vhd.uri}" -o table

You can change the info that you want if you can find it in the VM details through the CLI command az vm show. Hope this will help. Any more question you can give me the message.

You will need to use a query like below:

az vm show -g RG-DemoAutomation -n DemoVM101 --query "{VMName:name, admin:osProfile.adminUsername,nicId:networkProfile.networkInterfaces[0].id, osDiskId:storageProfile.osDisk.managedDisk.id}" -o table

As you mentioned you will need to iterate through the output of the az vm list and then for each VM you will need to run az vm show as I showed in the sample above.

You can have filter and data selection in one query as "[?query here].{customColName1:value1, customColName2:value2}" but the problem is that az vm list does not have the details that you are looking for. That detail is available with az vm show command.

For values in the above syntax, the query uses JMESPATH and you can find the exact value by using a neat utility available here: JMESPath Terminal

Also, try using the output as json (-o json) as that will be much more readable unless you want to dump the output into an excel sheet.

Related