Scripting dynamic memory, part 1: reading the configuration



With the availability of dynamic memory in Windows Server 2008 R2 SP1 there are some changes to how you need to handle memory as a scripter / developer.  The WMI API changes are now documented here http://msdn.microsoft.com/en-us/library/cc136856(VS.85).aspx

Most of the changes are fairly obvious – but two not so obvious properties are the properties for the “Startup RAM” and “Maximum RAM”.  In WMI these are mapped to “VirtualQuantity” and “Limit” respectively.  Here is a sample script that simply displays the memory configuration of the specified virtual machine:

# 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}
 
# Get the Memory setting data
$MemSetting = $SystemSettingData.getRelated("Msvm_MemorySettingData") | select -first 1
 
# Display information about the current memory configuration
switch ($MemSetting.DynamicMemoryEnabled)
   {
      True  {# Dynamic memory is enabled
             write-host
             write-host "Dynamic memory is enabled."
             write-host
             write-host "Startup Memory: " $MemSetting.VirtualQuantity $MemSetting.AllocationUnits
             write-host "Maximum Memory: " $MemSetting.Limit $MemSetting.AllocationUnits
             write-host "Memory buffer:  " $MemSetting.TargetMemoryBuffer
             write-host "Weight:         " $MemSetting.Weight}
                  
      False {# Dynamic memory is disabled
             write-host
             write-host "Dynamic memory is disabled."
             write-host
             write-host "Memory: " $MemSetting.VirtualQuantity $MemSetting.AllocationUnits
             write-host "Weight: " $MemSetting.Weight}
   }

Cheers,

Ben

ReadDMConfig.zip