Rodney Buike - Founder and original lazy admin. MVP: System Center Cloud and Datacenter Management

Daniel Nerenberg - Lazy admin 2.0. MVP: Windows Expert - IT Pro

Disclaimer

These postings are provided "AS IS" with no warranties, and confers no rights. You assume all risk for your use.

XenDesktop VDI and Dynamic Memory

XenDesktop is one of the leading VDI solutions right now due to its ability to run on the three major virtualization platforms.  When running Windows 7 SP1 VDI desktops with XenDesktop you may want to take advantage of Dynamic Memory for the workloads to increase VM density.  Unfortunately this cannot be done within XenDesktop at the time but you can do so via PowerShell. 

The following script created by Vlad Borodin and edited by my colleague Alex Khassanov will find all VMs on a Hyper-V host and enable Dynamic Memory on them.  You’ll need to know the Hyper-V host name, the prefix for the VDI VMs as well as the startup, maximum and buffer settings for Dynamic Memory. 

# PowerShell Script for changing RAM settings for several machines

# Vlad Borodin (vladboro@hotmail.com)

# 05 Mar, 2011

# Modified by AK, Jan-24-2012. Enable dynamic memory, set dynamic memory params

Write-Host("——————————————————–")

Write-Host("This script configures selected VMs with dynamic memory.")

write-host "You can press Enter to accept default values."

Write-Host("——————————————————–")

$ServerName = $(

              $selection = Read-Host("Enter Hyper-V Server Name (default – Hyperv02):")

              if ($selection) {$selection} else {"Hyperv02"})

$NameFilter = $(

              $selection = Read-Host("Enter VM Name Filter.Enter VDI- to affect only VDi machines. (default – VDI-):")

              if ($selection) {$selection} else {"VDI-"})

$Reservation = $(

              $selection = Read-Host("Enter Startup/Minimum RAM amount. 768 is recommended (default – 768):")

              if ($selection) {$selection} else {"768"})

$Limit = $(

              $selection = Read-Host("Enter Maximum RAM amount, 3072 is recommended (default – 3072):")

              if ($selection) {$selection} else {"3072"})

$Buffer = $(

              $selection = Read-Host("Enter memory buffer, 5-95, 20 recommended (default – 20):")

              if ($selection) {$selection} else {"20"})

$Weight = $(

              $selection = Read-Host("Enter memory weight, 1-10’000, 5000 recommended (default – 5000):")

              if ($selection) {$selection} else {"5000"})

$VMMS = Get-WmiObject -ComputerName $ServerName -namespace root\virtualization -class Msvm_VirtualSystemManagementService

$VMs = Get-WmiObject -ComputerName $ServerName -Namespace root\virtualization -class MSVM_ComputerSystem -Filter "ElementName like ‘%$NameFilter%’"

Write-Host("———————————-")

Write-Host("Please confirm that all is correct")

Write-Host("———————————-")

Write-Host("Hyper-V Server Name: ", $ServerName)

Write-Host("Startup/Minimum RAM: ", $Reservation)

Write-Host("Maximum RAM: ", $Limit)

Write-Host("Memory buffer: ", $Buffer)

Write-Host("Memory Weight: ", $Weight)

Write-Host("VMs to reconfigure")

foreach ($VM in $VMs)

              {

              Write-Host("    ",$Vm.ElementName)

              }

$waitstart = 200

$waitshutdown = 120

$selection = Read-Host("Proceed with dynamic memory configuration? (y/n)")

if ($selection -eq "y")

              {

              Write-Host("———————-")

              write-host("Shutting down all VMs.")

              Write-Host("———————-")

              foreach ($VM in $VMs)

                             {

                             Write-Host("Shutting down ", $VM.ElementName)

                             $shutdown = Get-WmiObject -ComputerName $ServerName -namespace root\virtualization -query "Associators of {$VM} where ResultClass=Msvm_ShutdownComponent" 

                             $result = $shutdown.InitiateShutdown($true,"Dynamic memory setup")

                             if ($result.returnvalue -match "0")

                                           {

#                                         start-sleep -s $waitshutdown

                                           write-host "- shutdown command sent" -foregroundcolor green

                                           write-host ""

                                           }

                             else

                                           {

                             write-host ""

                             write-host "unable to shutdown" -foregroundcolor red

                             write-host ""

                                           }

                             }

              $tmp = Read-Host("Ensure that all VMs are stopped. Press Enter to continue")

              Write-Host("———————–")

              write-host("Setting Dynamic Memory.")

              Write-Host("———————–")

              foreach ($VM in $VMs)

                             {

                             $VMSettings = Get-WmiObject -ComputerName $ServerName -Namespace root\virtualization -Query "Associators of {$VM} where ResultClass=Msvm_VirtualSystemSettingData"

                             $MemorySettings = Get-WmiObject -ComputerName $ServerName -Namespace root\virtualization -Query "Associators of {$VMSettings} where ResultClass=Msvm_MemorySettingData"

                             $MemorySettings.DynamicMemoryEnabled = 1

                             $MemorySettings.Reservation = $Reservation

                             $MemorySettings.VirtualQuantity = $Reservation

                             $MemorySettings.Limit = $Limit 

                             Write-Host("Changing memory configuration for ", $VM.ElementName)

                             $result = $VMMS.ModifyVirtualSystemResources($VM.__PATH, $MemorySettings.psbase.GetText(1))

                             }

              $tmp = Read-Host("Press Enter to start all VMs")

              Write-Host("——————–")

              write-host("Starting up all VMs.")

              Write-Host("——————–")

              foreach ($VM in $VMs)

                             {

                             Write-Host("Starting up ", $VM.ElementName)

                             $result = $VM.requeststatechange(2)

                             if ($result.returnvalue -match "0")

                                           {

#                                         start-sleep -s $waitstart

                                           write-host "- start up command sent" -foregroundcolor green

                                           write-host ""

                                           }

                             else

                                           {

                                           write-host ""

                                           write-host "unable to start up" -foregroundcolor red

                                           write-host ""

                                           }

                             }

              Write-Host("———")

              write-host("All done!")

              Write-Host("———")

              }

else

              {

              Write-Host("——————-")

              write-host "Script is canceled."

              Write-Host("——————-")

              }

Comments are closed.