azure-virtual-machineazure-python-sdk

How to filter virtual machines by running state in Python SDK?


I have a Python script that lists all VMs in a specific Azure resource group. The script collects and prints details such as VM name, location, size, and OS type.

Here is the current script:

from azure.identity import DefaultAzureCredential
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.resource import ResourceManagementClient

subscription_id = 'xxxxxxxxxx'
resource_group_name = 'yyyyyyyyyy'

credential = DefaultAzureCredential()
compute_client = ComputeManagementClient(credential, subscription_id)
resource_client = ResourceManagementClient(credential, subscription_id)

vm_list = compute_client.virtual_machines.list(resource_group_name)

vm_details_list = []

for vm in vm_list:
    vm_details = {
        'name': vm.name,
        'location': vm.location,
        'vm_size': vm.hardware_profile.vm_size,
        'os_type': vm.storage_profile.os_disk.os_type
    }
    vm_details_list.append(vm_details)

for vm in vm_details_list:
    print(f"VM Name: {vm['name']}")
    print(f"Location: {vm['location']}")
    print(f"VM Size: {vm['vm_size']}")
    print(f"OS Type: {vm['os_type']}")
    print('---')

I need to modify this script to only list VMs whose state is 'running'. How can I achieve this? Thank you for your help!


Solution

  • I have few virtual machines in my resource group which are in 'Running' state as below:

    enter image description here

    To filter Azure virtual machines by 'Running' state, you can make use of below modified python code:

    from azure.identity import DefaultAzureCredential
    from azure.mgmt.compute import ComputeManagementClient
    from azure.mgmt.resource import ResourceManagementClient
    
    subscription_id = 'subId'
    resource_group_name = 'rgName'
    
    credential = DefaultAzureCredential()
    
    compute_client = ComputeManagementClient(credential, subscription_id)
    resource_client = ResourceManagementClient(credential, subscription_id)
    
    vm_list = compute_client.virtual_machines.list(resource_group_name)
    
    # Function to get the power state of a VM
    def get_vm_power_state(vm):
        instance_view = compute_client.virtual_machines.instance_view(resource_group_name, vm.name)
        for status in instance_view.statuses:
            if status.code.startswith('PowerState/'):
                return status.display_status
        return 'Unknown'
    
    # Collect details of running VMs
    running_vms = []
    
    for vm in vm_list:
        power_state = get_vm_power_state(vm)
        if power_state == 'VM running':
            vm_details = {
                'name': vm.name,
                'location': vm.location,
                'vm_size': vm.hardware_profile.vm_size,
                'os_type': vm.storage_profile.os_disk.os_type, 
                'status': power_state
            }
            running_vms.append(vm_details)
    
    for vm in running_vms:
        print(f"VM Name: {vm['name']}")
        print(f"Location: {vm['location']}")
        print(f"VM Size: {vm['vm_size']}")
        print(f"OS Type: {vm['os_type']}")
        print(f"Status: {vm['status']}")
        print()
    

    Response:

    enter image description here