Scripting dynamic memory, part 2: displaying current usage information

Moving on in my “scripting dynamic memory” series, the next thing to look at is how to get the current memory usage and memory available data that we display in the Hyper-V manager.  You can get the current memory usage by just looking at MSVM_Memory (https://msdn.microsoft.com/en-us/library/cc136855(v=VS.85).aspx), but unfortunately you cannot get the memory availability that way.  To get the memory availability you need to use GetSummaryInformation (https://msdn.microsoft.com/en-us/library/cc160706(v=VS.85).aspx).

GetSummaryInformation is an API that we use as a light weight way to get a bunch of information about a virtual machine.  It returns a Msvm_SummaryInformation Class (or a collection of these) (https://msdn.microsoft.com/en-us/library/cc136898(v=VS.85).aspx)

 # Prompt for the Hyper-V Server to use
 $HyperVServer = Read-Host "Specify the Hyper-V Server to use (enter '.' for the local computer)"
  
 # Prompt for the virtual machine to use
 $VMName = Read-Host "Specify the name of the virtual machine"
  
 # Get the management service
 $VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $HyperVServer
  
 # Get the virtual machine object
 $VM = gwmi MSVM_ComputerSystem -filter "ElementName='$VMName'" -namespace "root\virtualization" -computername $HyperVServer
  
 # SettingType = 3 ensures that we do not get snapshots
 $SystemSettingData = $VM.getRelated("Msvm_VirtualSystemSettingData") | where {$_.SettingType -eq 3}
  
 # Request the virtual machine state (100), current memory (103) and memory availability (112)
 $RequestedInformation = 100,103,112
  
 # Get the summary information for just the selected virtual machine
 $SummaryInformation = $VMMS.GetSummaryInformation($SystemSettingData, $RequestedInformation).SummaryInformation | select -first 1
      
 # Check that the virtual machine is running
 if ($SummaryInformation.EnabledState -eq "2")
    { write-host "Memory information for" $VMName
      write-host
      write-host "Current memory usage:" $SummaryInformation.MemoryUsage "MB"
      
      # If memory available == 2147483647 then no memory available value has been returned from the virtual machine
      if ($SummaryInformation.MemoryAvailable -ne 2147483647)
         { write-host "Current memory availability:" $SummaryInformation.MemoryAvailable"%"}
      else
         { write-host "Dynamic memory is not currently active on this virtual machine"}
    }
 else
    { write-host "The requested virtual machine is not currently running" }
  

Some things to know:

  • While the code above gets the summary information for a single virtual machine – GetSummaryInformation allows you to pass in a null value for the system setting data.  In this case you will get information about all virtual machines on the system.
  • While I am only displaying information about the current memory and the memory availability – you can get a lot more information in this way.  Hit the link for GetSummaryInformation above if you want to see all that you can possibly get in this fashion.

Cheers,

Ben

DisplayDM.zip