Sign in
agent:

List all Azure VMs using cli commands

There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.

This task displays information about all virtual machines within a specific azure subscription and provides essential details such as VM names, their resource groups, locations, and creation times. It's a vital process for cloud administrators to manage and monitor their VM infrastructure effectively, enabling informed decisions about resource utilization and infrastructure management.

import json def process_vm_data(vm_data): processed_vms = [] for vm in vm_data: vm_details = { 'vm_name': vm['name'], 'resource_group': vm['resourceGroup'], 'location': vm['location'], 'vm_size': vm['hardwareProfile']['vmSize'], 'os_type': vm['storageProfile']['osDisk']['osType'], 'os_disk_name': vm['storageProfile']['osDisk']['name'], 'os_disk_id': vm['storageProfile']['osDisk']['managedDisk']['id'] } processed_vms.append(vm_details) return processed_vms try: result = _exe(None, "az vm list") op = json.loads(result) #print(json.dumps(op,indent=4)) # for debugging # Processing the VM data processed_vms = process_vm_data(op) #print(processed_vms) # for debugging # Printing the processed VM details for downstream tasks for vm in processed_vms: #print(json.dumps(vm, indent=4)) print("VM Details") print("==========") print(f"VM Name: {vm['vm_name']}") print(f"Resource Group: {vm['resource_group']}") print(f"Location: {vm['location']}") print(f"VM Size: {vm['vm_size']}") print(f"OS Type: {vm['os_type']}") print(f"OS Disk Name: {vm['os_disk_name']}") print(f"OS Disk ID: {vm['os_disk_id']}") print("-" * 30) # Separator for readability between VMs except json.JSONDecodeError: print("Error decoding JSON response from Azure CLI.")
copied