Freigeben über


Getting VM Memory Usage and Demand Programmatically

A couple of people have recently asked me how to get virtual machine memory usage and memory demand programmatically.  What I am referring to is the information that we display here in Hyper-V Manager:

image

I have blogged about this in the past – but things have changed a bit since then – so I thought I would post an updated code sample.  Right here:

There are two main things to call out about this sample:

  • This information is all exposed on Msvm_SummaryInformation – which is documented here.
  • Memory demand is actually reported as a percentage of the used memory in the underlying API.  So to get the same information as is displayed by Hyper-V Manager – you have to do some math (included in the sample above)

Cheers,
Ben

Comments

  • Anonymous
    June 13, 2017
    Hi Ben, Thank you very much for this nice and perfect script. Sometimes it returns a value similar with this one: Current memory demand: -87960926085.12 MB Can you please help me to understand this value. Thank you Regards,Aurelian
  • Anonymous
    October 24, 2017
    This is OT - but after installing 1709 on my Windows 10 PC and updating the RSAT tools, hyper-v manager can no longer connect to windows 2012 Hosts (RPC Server unavailable) - is fine with Windows 2016 hosts.