首页 > 解决方案 > 如何计算 Azure VM 中的内存总数

问题描述

我有 Azure 帐户,我需要知道所有 VM 中安装了多少内存。对于核心数,我使用以下命令。

>Get-AzureRmVMUsage -Location WestUS

但是我怎样才能获得内存详细信息?

标签: azurepowershell-3.0powershell-v6.0

解决方案


举个例子:如何获取每个VM的已安装内存,注意如果要计算所有VM的已安装内存总数,只需将它们一一相加即可。

#get all the vms in the resource group, you also can get all the vms in your subscription by removing -ResourceGroupName "xxx"
$vms = Get-AzureRmVM -ResourceGroupName "xxx" -status

foreach($vm in $vms)
{
  $temp = Get-AzureRmVMSize -ResourceGroupName $vm.ResourceGroupName -VMName $vm.name | where{$_.Name -eq $vm.HardwareProfile.VmSize}

  #get each vm's installed memory
  $vm_memory = $temp.MemoryInMB

  Write-Output "VM Name: $($vm.name); VM Memory: $vm_memory"

  #if you want to count all the vms' memory, you can write your own logic here
}

测试结果:

在此处输入图像描述


推荐阅读