识别与释放的VM相关的资源

时间:2019-01-10 20:53:41

标签: azure azure-cli

在Azure VM的清理过程中。我正在尝试识别与Deallocated VM相关的所有资源,例如网络,存储和nic。

我在查询下面运行以获取详细信息,但是无法在同一查询参数中编写查询以获取其他详细信息以表格形式存储nic,storge。

az vm列表-d --query“ [?powerState =='VM分配的地址']” -o表

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

任何帮助都将是可贵的,我特别在寻找az客户查询。由于VM释放列表很大,因此我将通过gitlab管道运行。

},
"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 个答案:

答案 0 :(得分:0)

您将需要使用如下查询:

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

如前所述,您将需要遍历az vm list的输出,然后对于每个VM,都需要运行az vm show,如我在上面的示例中所示。

您可以在一个查询中将过滤器和数据选择显示为“ [?query here].{customColName1:value1, customColName2:value2}”,但问题是az vm list没有要查找的详细信息。该详细信息可通过az vm show命令获得。

对于上述语法中的,查询使用JMESPATH,您可以使用以下可用的简洁实用工具找到确切的值:JMESPath Terminal

另外,请尝试将输出用作json(-o json),因为除非您想将输出转储到excel工作表中,否则它将更具可读性。

答案 1 :(得分:0)

Azure中的VM有两种类型,一种是托管VM,另一种是非托管VM。要显示所有VM的详细信息时,应注意这一点。

此外,该存储帐户没有任何属性。您可以将文件存储在存储帐户中,并且不与虚拟机关联。因此,如果虚拟机不受管理,您只会获得有关存储帐户的一些信息。

获取具有某些资源的VM列表,例如vmName,NIC,osDisk或osDiskURI,CLI命令在此处:

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

如果可以通过CLI命令az vm show在VM详细信息中找到它,则可以更改所需的信息。希望这会有所帮助。您还有其他问题可以给我留言。