You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Python SDK筛选处于运行状态的Azure虚拟机

如何修改Python脚本仅列出Azure资源组中运行状态的虚拟机

我有一个Python脚本,可列出特定Azure资源组中的所有虚拟机(VM),并收集打印VM名称、位置、规格、操作系统类型等信息。当前脚本如下:

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('---')

需要修改此脚本,使其仅列出状态为running的VM,实现方法如下:

核心思路

默认的virtual_machines.list()仅返回VM的基础配置信息,不包含运行状态。需要通过virtual_machines.get()方法并指定expand='instanceView'参数,获取包含VM运行状态的实例视图,然后筛选出状态为VM running的机器。

修改后的完整脚本

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的实例视图,包含运行状态信息
    vm_instance = compute_client.virtual_machines.get(
        resource_group_name,
        vm.name,
        expand='instanceView'
    )
    
    # 从实例视图的状态列表中提取电源状态
    power_state = next(
        status.display_status for status in vm_instance.instance_view.statuses
        if status.code.startswith('PowerState/')
    )
    
    # 仅保留运行中的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
        }
        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('---')

关键代码说明

  1. 获取实例视图:调用compute_client.virtual_machines.get()时添加expand='instanceView',让返回结果包含VM的运行状态等实时信息。
  2. 筛选电源状态:从实例视图的statuses列表中找到以PowerState/开头的状态项,其display_status为VM running时表示虚拟机处于运行状态。
  3. 条件过滤:仅当VM状态为运行中时,才将其信息加入结果列表。

内容的提问来源于stack exchange,提问作者user15042420

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 02:43:24