# modded version from http://www.yellow-bricks.com/2010/02/24/custom-shares-on-a-resource-pools-scripted/ param( [string]$ClusterName, [int]$GoldPer, [int]$SilverPer, [int]$BronzePer, [int]$CopperPer, [int]$action = "0") if (!$ClusterName){Write-Host -ForegroundColor Red "Cluster Name is mandatory";break} if (!$GoldPer -or !$SilverPer -or !$BronzePer -or !$CopperPer){Write-Host -ForegroundColor Red "all RP% are mandatory";break} if ($GoldPer+$SilverPer+$BronzePer+$CopperPer -ne "100"){Write-Host -ForegroundColor Red "RP% sum is not equal to 100%";break} if ($GoldPer -eq 0 -or $SilverPer -eq 0 -or $BronzePer -eq 0 -or $CopperPer -eq 0){Write-Host -ForegroundColor Red "RP% can not be 0%";break} # We need to specify the cluster name as the same Resource Pool names might exist within # multiple clusters $Cluster = $ClusterName # IMPORTANT NOTE # Only specify resource pools that are in the same level within the resource pool heirarchy. # Do not specify a combination of parent and child pools. # These resource pools must already exist within the cluster specified above. # Specify a comma seperated collection of resource pools $ResourcePools = @("GOLD", "SILVER", "BRONZE", "COPPER") #The share values you specify below will be used to determine the weighting of resources available #to each pool, but will not be the actual share values applied. $PoolCPUShares = @($GoldPer, $SilverPer, $BronzePer, $CopperPer) $PoolMemShares = @($GoldPer, $SilverPer, $BronzePer, $CopperPer) #Specify the maximum share value we want to apply. The highest priority resource pool will be set #to this value. Other pools will be proportionally lower, based on the $PoolCPUShares, $PoolMemshares #and workloads present within the resource pools $SharesUpperLimit = 8000 #Specify the minimum share value you want applied to each resource pool. This will ensure that empty #resource pools will not end up with zero shares. $MinPoolCPUshares = 100 $MinPoolMemShares = 100 #Do we want the CPU share weighting based on the number of virtual machines within the resource pool, or the number of vCPUs? #(vCPUs is the default method used by vSphere when specifying Low, Medium or High shares on a per-VM basis) $CountvCPUs = $true ####################################################################################### ############## Do not modify anything beyond this point ############################## ####################################################################################### # Connect-VIServer $vCenterServer -User $User -Password $Password $ArrayIndex = 0 $NumvCPUs = new-object object[] $ResourcePools.length $NumVMs = new-object object[] $ResourcePools.length $TotalMemory = new-Object object[] $ResourcePools.length $ArrayIndex = 0 Foreach ($ResourcePool in $ResourcePools) { $NumvCPUs[$arrayindex] =0 $NumVMs[$ArrayIndex] =0 $TotalMemory[$ArrayIndex] = 0 $pool = Get-ResourcePool -Name $ResourcePool -location $Cluster Foreach ($VM in ($pool |Get-VM | where {$_.PowerState -eq "PoweredOn"})) #We only care about running VMs { #Count the number of allocated vCPUs within the Resource Pool $NumvCPUs[$arrayindex] += ($VM).NumCpu #Count the number of VMs $NumVMs[$arrayindex] += 1 # Count the total memory allocated within the Resource Pool $TotalMemory[$ArrayIndex] += ($VM).MemoryMB } # Write-Host "Discovered " $NumvCPUs[$arrayindex] "running vCPUs in resource pool " $ResourcePool # Write-Host "Discovered " $NumVMs[$arrayindex] "running virtual machines in resource pool " $ResourcePool # Write-Host $TotalMemory[$arrayIndex] "MB of memory allocated in resource pool" $ArrayIndex += 1 } #Calculate memory shares for ($i=0; $i -lt ($PoolMemShares.length); $i++) { $PoolMemShares[$i] = [int]$PoolMemShares[$i] * [int]$TotalMemory[$i] } #Calculate CPU shares # #If $CountvCPUs has been defined as true we will base the shares on the number of vCPUs we found in the resource pool. This #is the standard mechanism used for CPU share values within VI3 and vSphere and is highly recommmended. Otherwise we will just count #number of VMs and ignore the number of vCPUs for ($i=0; $i -lt ($PoolCPUShares.length); $i++) { if ($CountvCPUs) { $PoolCPUShares[$i] = [int]$PoolCPUShares[$i] * [int]$numvCPUs[$i] } else { $PoolCPUShares[$i] = [int]$PoolCPUShares[$i] * [int]$numVMs[$i] } } # Find the largest array members so we can set the shares to a sensible value $MaxMemShares = $PoolMemShares[0] for ($i=0; $i -lt $PoolMemShares.length; $i++) { if ($PoolMemShares[$i] -gt $MaxMemShares) { $MaxMemShares = $PoolMemShares[$i] } } $MaxCPUShares = $PoolCPUShares[0] for ($i=0; $i -lt $PoolCPUShares.length; $i++) { if ($PoolCPUShares[$i] -gt $MaxCPUShares) { $MaxCPUShares = $PoolCPUShares[$i] } } If ($MaxCPUShares -gt 0) { #Set the highest share to a maximum of $SharesUpperLimit. All other shares will be a proprtional value of $SharesUpperLimit $CPUShareMultiplier = $SharesUpperLimit / $MaxCPUShares for ($i=0; $i -lt $PoolCPUShares.length; $i++) { $PoolCPUShares[$i] = [int]($PoolCPUShares[$i] * $CPUShareMultiplier) #If we're below the minimum, readjust if ($PoolCPUShares[$i] -lt $MinPoolCPUshares) { $PoolCPUShares[$i] = $MinPoolCPUshares} Write-Host "Resource Pool " $ResourcePools[$i] " : " $PoolCPUShares[$i] " CPU shares" } } else { Write-Host "Warning: No running VMs found within cluster or CPU shares have been defined as 0 (zero)" #Set it to the minimum specified by the user for ($i=0; $i -lt $PoolCPUShares.length; $i++) { $PoolCPUShares[$i] = $MinPoolCPUshares Write-Host "Resource Pool " $ResourcePools[$i] " : " $PoolCPUShares[$i] " CPU shares" } } if ($MaxMemShares -gt 0) { $MemShareMultiplier = $SharesUpperLimit / $MaxMemShares for ($i=0; $i -lt $PoolMemShares.length; $i++) { $PoolMemShares[$i] = [int]($PoolMemShares[$i] * $MemShareMultiplier) #If we're below the minimum, readjust if ($PoolMemShares[$i] -lt $MinPoolMemshares) { $PoolMemShares[$i] = $MinPoolMemshares} Write-Host "Resource Pool " $ResourcePools[$i] " : " $PoolMemShares[$i] " memory shares" } } else { Write-Host "Warning: No running VMs found or Memory shares have been defined as 0 (zero)" #Set it to the minimum specified by the user for ($i=0; $i -lt $PoolMemShares.length; $i++) { $PoolMemShares[$i] = $MinPoolMemshares Write-Host "Resource Pool " $ResourcePools[$i] " : " $PoolMemShares[$i] " memory shares" } } #Loop through each Resource Pool and set the CPU and memory shares that have been calculated $ArrayIndex = 0 if ($action -eq "1"){ Foreach ($ResourcePool in $ResourcePools) { $pool = Get-ResourcePool -Name $ResourcePool -location $Cluster if ($pool.NumCpuShares -notmatch $PoolCPUShares[$ArrayIndex] -or $pool.NumMemShares -notmatch $PoolMemShares[$ArrayIndex]) {Set-Resourcepool -Resourcepool $Pool -CPUsharesLevel Custom -NumCpuShares $PoolCPUShares[$ArrayIndex] -MemSharesLevel Custom -NumMemShares $PoolMemShares[$ArrayIndex]|Out-Null} $ArrayIndex += 1 } Write-Host -ForegroundColor Yellow "Done!" }