Using WMI to determine memory usage by Virtual Server

When you create a virtual machine under Virtual Server, you configure the amount of memory that you want that virtual machine to believe that it has (say 512mb of RAM).  However, Virtual Server needs to use more than that amount of memory to actually run the virtual machine.  Virtual Server also needs memory for itself to run emulated devices and store various information about the virtual machine in.  Unfortunately, because Virtual Server uses a driver (VMM.SYS) to allocate the memory this information is not easily to determine (i.e. it does not show up in task manager).  In the past I posted about how to use PerfMon to see how much memory Virtual Server was actually using, but today I have a nice little VB Script that talks to the Virtual Server WMI interfaces to determine this information.

Now – the fact that Virtual Server has WMI interfaces is very poorly documented – but they do exist and a primarily for gathering statistical information from Virtual Server.  Virtual Server uses a WMI namespace of \rootvmvirtualserver, and this script connects to that namespace on the local computer, gets a collection of all of the virtual machine objects and then lists out the ‘PhysicalMemoryAllocated’ property of each virtual machine:

Option Explicit

Dim strComputer, objWMIService, colVMs, vm, totalMem

totalMem = 0
strComputer = “.”
Set objWMIService = GetObject(“winmgmts:\” & strComputer & “rootvmvirtualserver”)
Set colVMs = objWMIService.ExecQuery( _
    “SELECT * FROM VirtualMachine”,,48)
For Each vm in colVMs
    Wscript.Echo “———————————–“
    Wscript.Echo & “:: Physical Memory Allocated: ” & _
                        vm.PhysicalMemoryAllocated / 1048576 & _
   ” MB (” & vm.PhysicalMemoryAllocated & ” bytes)”
    totalMem = totalMem + vm.PhysicalMemoryAllocated

    Wscript.Echo “———————————–“
    Wscript.Echo “All virtual machines”
    Wscript.Echo “———————————–“
    Wscript.Echo “Physical Memory Allocated: ” & totalMem / 1048576 _
   & ” MB (” & totalMem & ” bytes)”