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

Azure资源图查询VM核心数返回空值求助及PowerShell方案需求

解决Azure虚拟机核心数查询问题

Azure资源图查询修复方案

原查询中sku.capacity字段不适用于单个虚拟机(该字段用于虚拟机规模集的实例数量统计),因此返回全null值。以下是两种正确的查询方式:

方式1:基于Resources表解析VM规格

Resources
| where type =~ 'microsoft.compute/virtualmachines'
| extend vmSize = tostring(properties.hardwareProfile.vmSize)
| extend CoreCount = toint(extract(@'(\d+)'@, 1, vmSize))
| project subscriptionId, name, location, resourceGroup, CoreCount, VmSize = vmSize
| order by CoreCount desc

方式2:使用专用Azure.VirtualMachines表(更简洁准确)

Azure.VirtualMachines
| project subscriptionId, name, location, resourceGroup, CoreCount = properties.hardwareProfile.vmSizeCoreCount, VmSize = properties.hardwareProfile.vmSize
| order by CoreCount desc

PowerShell导出CSV解决方案

使用Az模块获取全量虚拟机信息并导出核心数到CSV文件:

  1. 若未安装Az模块,先执行安装命令:
Install-Module -Name Az -AllowClobber -Scope CurrentUser
  1. 执行以下脚本完成数据采集与导出:
# 登录Azure账号
Connect-AzAccount

# 获取所有虚拟机及核心数信息
$vmData = Get-AzVM | Select-Object `
    @{Name='SubscriptionId'; Expression={$_.Id.Split('/')[2]}},
    Name,
    Location,
    ResourceGroupName,
    @{Name='CoreCount'; Expression={[regex]::Match($_.HardwareProfile.VmSize, '\d+').Value -as [int]}},
    @{Name='VmSize'; Expression=$_.HardwareProfile.VmSize}

# 导出到CSV文件
$vmData | Export-Csv -Path "VM_Core_Inventory.csv" -NoTypeInformation -Encoding UTF8

执行完成后,当前目录下会生成VM_Core_Inventory.csv文件,包含所需的虚拟机核心数及关联信息。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 23:20:40