Looking at Memory Usage on Hosts when Creating a New VM

Looking at Memory Usage on Hosts when Creating a New VM

Rate This
  • Comments 3

In my house I have two Hyper-V servers running multiple virtual machines with dynamic memory enabled.  This can make it a bit tricky when I want to create a new virtual machine - and I need to figure out the best server to use.  Thankfully - this little bit of PowerShell comes to the rescue:

 "Hyper-V-1", "Hyper-V-2" | %{"Memory Available on " + $_ + " : " + ("{0:N2}" -f (((get-vmhost $_).MemoryCapacity / 1GB) - ((get-vm -computername $_ | measure MemoryAssigned -sum).sum / 1GB))).ToString() + " GB"}

"Hyper-V-1" and "Hyper-V-2" are the names of my hosts.  This snippet gets the total memory in each host, subtracts the memory currently being used by virtual machines, and shows the results.  Like this:

So I can see that Hyper-V-2 has more memory available right now.

This is obviously a simple approach (it does not account for changing memory demand) but it is quick and easy to do.

Cheers,
Ben

Leave a Comment
  • Please add 4 and 8 and type the answer here:
  • Post
  • Thanks a lot. Helped me to get the information I needed

  • Ben, I was searching more and found a tool from which seems to be able doing same things via GUI: hyperv.veeam.com/free-task-manager-hyper-v-performance-monitoring

    Would you recommend it?

  • Hi Ben,

    Its really a good one.

    I tried to make it better by changing the input. Instead of giving each server name, We could use the cluster itself and fetch the cluster nodes through that. Made a quick sorting based on available memory. Also included the number of VMs on each host which also is a consideration while placing a new VM.

    (Get-ClusterNode -cluster ClusterName |Select Name, @{L="VMCount";E={(Get-VM -ComputerName $_.name).count}}, @{L="AvailableMemory"; Expression={(Get-VMHOST $_.Name).MemoryCapacity/1GB-(Get-VM -ComputerName $_.name |Measure-Object MemoryAssigned -Sum).sum/1GB}}|Sort-Object AvailableMemory -Descending)

    Cheers !

    Shaba

    insidevirtualization.com

Page 1 of 1 (3 items)