# encoding: ascii
# api: powershell
# title: Set Resource Limits
# description: Set the resource limits on VMs automatically based on the number of vCPUs and vRAM assigned. This is useful as a preliminary pass at resource tiering.
# version: 0.1
# author: Ed Goad
# license: CC0
# x-poshcode-id: 3392
# x-archived: 2012-05-10T14:18:02
# x-published: 2012-05-02T13:55:00
#
# This script identifies the number of vCPUs and amount of RAM assigned to a VM, then configures limits of 1000MHz per vCPU, and a reservation of 25% of the RAM.
#
Param([Parameter(Mandatory=$true)] [string]$VMGuest)
$vm = get-vm $VMGuest
$cpuCap = $vm.NumCPU*1000
$cpuRes = $cpuCap/2
$memCap = $vm.MemoryMB
$memRes = $memCap/4
$spec = new-object VMware.Vim.VirtualMachineConfigSpec;
$spec.memoryAllocation = New-Object VMware.Vim.ResourceAllocationInfo;
$spec.memoryAllocation.Shares = New-Object VMware.Vim.SharesInfo;
$spec.memoryAllocation.Limit = -1;
$spec.memoryAllocation.Reservation = $memRes;
$spec.cpuAllocation = New-Object VMware.Vim.ResourceAllocationInfo;
$spec.cpuAllocation.Limit = $cpuCap;
$spec.cpuAllocation.Reservation = $cpuRes;
($vm | get-view).ReconfigVM_Task($spec)