Change vRAM for multiple VMs in vCenter

Video RAM is one of those funny things you don’t think about too often, but if you need more than the default 4MB, it can be a pain to change. VM vRAM size affects the size of the console window among other things. This console size is important for VDI, using remote access tools like WebEx and GotoMeeting and for viewing the console in the VMware Workstation interface.

So, when I install a new set of VMs, I usually set the vRAM size to a larger number to accommodate at least 1920 x 1080 (the size of my main monitor) which is about 256MB.

I found this great function by Al Renouf and wrote a script to apply it to my VMs. Needless to say, you will need PowerCLI to make this work. Also, as the notes in the script caution, this will change all VMs in a given datacenter, except the vCenter server and one other specified server. USE WITH CAUTION! I usually run this after I deployed a fresh Datacenter, so it’s not a problem.

<#
Title: 		Set the video RAM size on all VMs in a Datacenter
Author: 	Michael Kenning (mjkenning@gmail.com) -- Function source: http://virtu-al.net
Version: 	1.12 (release)
Updated: 	6 MAY 2015

Usage: 		.\set-VMvRAM.ps1 [VCENTER IP or FQDN] [vRAM size in MB as int]
NOTES:		Change variables as needed. WARNING, this script will power off multiple VMs. USE WITH CAUTION!
#>

param (
	[Parameter(Mandatory=$true,Position=0)]
	[ValidateNotNullOrEmpty()]
	[String]
	$vCenter,
	[Parameter(Mandatory=$true,Position=1)]
	[ValidateNotNullOrEmpty()]
	[int]
	$vRAMsize
)

### VARIABLES ###
$noUpdate = "DCVM1"
$dataCenter = "DATACENTER"
### END VARIABLES ###

### BEGIN FUNCTIONS ###
function Set-VMVideoMemory {
<# .SYNOPSIS   Changes the video memory of a VM
	.DESCRIPTION   This function changes the video memory of a VM 
	.NOTES   Source: http://virtu-al.net   Author: Alan Renouf   Version: 1.1 
	.PARAMETER VM   Specify the virtual machine 
	.PARAMETER MemoryMB   Specify the memory size in MB 
	.EXAMPLE   PS> Get-VM VM1 | Set-VMVideoMemory -MemoryMB 4 -AutoDetect $false
#>
 
  Param (
	[parameter(valuefrompipeline = $true, mandatory = $true, HelpMessage = "Enter a vm entity")]
	[VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl]$VM,
	[int64]$MemoryMB,
	[bool]$AutoDetect,
	[int]$NumDisplays
	)
 
  Process {
   $VM | Foreach {
      $VideoAdapter = $_.ExtensionData.Config.Hardware.Device | Where {$_.GetType().Name -eq "VirtualMachineVideoCard"}
      $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
      $Config = New-Object VMware.Vim.VirtualDeviceConfigSpec
      $Config.device = $VideoAdapter
      If ($MemoryMB) {
         $Config.device.videoRamSizeInKB = $MemoryMB * 1KB
      }
      If ($AutoDetect) {
         $Config.device.useAutoDetect = $true
      } Else {
         $Config.device.useAutoDetect = $false
      }
      Switch ($NumDisplays) {
         1{ $Config.device.numDisplays = 1}
         2{ $Config.device.numDisplays = 2}
         3{ $Config.device.numDisplays = 3}
         4{ $Config.device.numDisplays = 4}
         Default {}
      }
      $Config.operation = "edit"
      $spec.deviceChange += $Config
      $VMView = $_ | Get-View
      Write-Host "Setting Video Display for $($_)"
      $VMView.ReconfigVM($spec)
   }
  }
}

### END FUNCTIONS ###

# -- Connect to the vCenter server
Connect-VIServer -Server $vCenter

# -- Get a list of VMs from the specified Datacenter
$vms = Get-Datacenter $dataCenter | Get-VM

# -- Loop through the list of VMs and do stuff
Foreach ($vm in $vms) {
	$vmname = $vm.Name
	$vmStatus1 = $null
	
	# -- Don't update the vCenter server!
	if ($vmname -ne $vCenter) {
		
		# -- Don't update the specified server (i.e. domain controller)
		if ($vmname -ne $noUpdate) {
			
			# -- Check to see if the VM is powered On
			$vmStatus = Get-VM $vmname | ForEach{$_.PowerState}
			
			# -- If it's powered on, shut it down gracefully
			if ($vmStatus -eq "PoweredOn") {
				$vmStatus1 = $vmStatus
				Shutdown-VMGuest $vmname -Confirm:$false
				do {
					$vmStatus = Get-VM $vmname | ForEach{$_.PowerState}
					write-host "Checking power status for $vm. Please Wait."
					sleep 5
				}
			until ($vmstatus -eq "PoweredOff")
			}
			
			# -- Change the vRAM size to the specified amount
			Get-VM $vm | Set-VMVideoMemory -MemoryMB $vRAMsize -AutoDetect $false
			
			# -- If the VM was powered on when we started, power it back on
			If ($vmStatus1 -eq "PoweredOn") {
				start-vm $vmname
			}
		}
	}
}
Advertisements
Tagged with: ,
Posted in PowerCLI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: