####################################################################################### # Copyright Andrew Mitchell 2009 # # You may freely use and redistribute this script as long as this # copyright notice remains intact # # # DISCLAIMER. THIS SCRIPT IS PROVIDED TO YOU "AS IS" WITHOUT WARRANTIES OR CONDITIONS # OF ANY KIND, WHETHER ORAL OR WRITTEN, EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY # DISCLAIMS ANY IMPLIED WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY # QUALITY, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. # # Sourced from http://www.yellow-bricks.com/2010/02/24/custom-shares-on-a-resource-pools-scripted # # Modified by Doug Youd 20/12/2010 - Added Ability to do multiple clusters, secure # credential storage, changed share ratio and fixed a couple of typos. # ####################################################################################### ############## Modify the following variables to suit your environment ############### $vCenterServer = "" $CredsLocation = "c:\tmp\Weighted_Shares_Creds.txt" $User = "Domain\UserName" # We need to specify the cluster name as the same Resource Pool names might exist within # multiple clusters $Clusters = @("cluster 01","Cluster 02") #All Clusters? (This setting will overide the manual setting above. $RECURSECLUSTERS = $true #OR $false # 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") #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 = @(50, 35, 15) $PoolMemShares = @(50, 35, 15) #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 = 80000 #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 = 1000 $MinPoolMemShares = 1000 #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 ############################## ####################################################################################### #load Vmware Module Add-PSSnapin VMware.VimAutomation.Core #Get Credentials and store them securely if(Test-Path $CredsLocation) { $Password = Get-Content $CredsLocation | ConvertTo-SecureString $Creds = New-Object System.Management.Automation.PsCredential $User, $Password } else { Write-Output "No Cached credentials found in $CredsLocation, Please Enter the Password for username $UserName" $Creds = Get-Credential -Credential $User $Creds.Password |ConvertFrom-SecureString | Set-Content $CredsLocation } Connect-VIServer -server $vCenterServer -Credential $Creds -ErrorAction Stop #Recurse through all clusters? if($RECURSECLUSTERS) { #Clear the pre-allocated Servers $Clusters = @() foreach($Cluster in Get-Cluster -Server $vCenterServer) { $Clusters += [string]$Cluster.Name } Clear-Variable Cluster } foreach($Cluster in $Clusters) { $CalcPoolMemShares = @() $CalcPoolCPUShares = @() $CalcPoolMemShares += $PoolMemShares $CalcPoolCPUShares += $PoolCPUShares $MaxCPUShares = 0 $MaxMemShares = 0 $CPUShareMultiplier = 0 $MemShareMultiplier = 0 $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++) { $CalcPoolMemShares[$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) { $CalcPoolCPUShares[$i] = [int]$PoolCPUShares[$i] * [int]$numvCPUs[$i] } else { $CalcPoolCPUShares[$i] = [int]$PoolCPUShares[$i] * [int]$numVMs[$i] } } # Find the largest array members so we can set the shares to a sensible value $MaxMemShares = $CalcPoolMemShares[0] for ($i=0; $i -lt $PoolMemShares.length; $i++) { if ($CalcPoolMemShares[$i] -gt $MaxMemShares) { $MaxMemShares = $CalcPoolMemShares[$i] } } $MaxCPUShares = $CalcPoolCPUShares[0] for ($i=0; $i -lt $CalcPoolCPUShares.length; $i++) { if ($CalcPoolCPUShares[$i] -gt $MaxCPUShares) { $MaxCPUShares = $CalcPoolCPUShares[$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++) { $CalcPoolCPUShares[$i] = [int]($CalcPoolCPUShares[$i] * $CPUShareMultiplier) #If we're below the minimum, readjust if ($CalcPoolCPUShares[$i] -lt $MinPoolCPUshares) { $CalcPoolCPUShares[$i] = $MinPoolCPUshares} Write-Host "Resource Pool " $ResourcePools[$i] " : " $CalcPoolCPUShares[$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++) { $CalcPoolCPUShares[$i] = $MinPoolCPUshares Write-Host "Resource Pool " $ResourcePools[$i] " : " $CalcPoolCPUShares[$i] " CPU shares" } } if ($MaxMemShares -gt 0) { $MemShareMultiplier = $SharesUpperLimit / $MaxMemShares for ($i=0; $i -lt $PoolMemShares.length; $i++) { $CalcPoolMemShares[$i] = [int]($CalcPoolMemShares[$i] * $MemShareMultiplier) #If we're below the minimum, readjust if ($CalcPoolMemShares[$i] -lt $MinPoolMemshares) { $CalcPoolMemShares[$i] = $MinPoolMemshares } Write-Host "Resource Pool " $ResourcePools[$i] " : " $CalcPoolMemShares[$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++) { $CalcPoolMemShares[$i] = $MinPoolMemshares Write-Host "Resource Pool " $ResourcePools[$i] " : " $CalcPoolMemShares[$i] " memory shares" } } #Loop through each Resource Pool and set the CPU and memory shares that have been calculated $ArrayIndex = 0 Foreach ($ResourcePool in $ResourcePools) { $pool = Get-ResourcePool -Name $ResourcePool -location $Cluster Set-Resourcepool -Resourcepool $Pool -CPUsharesLevel Custom -NumCpuShares $CalcPoolCPUShares[$ArrayIndex] -MemSharesLevel Custom -NumMemShares $CalcPoolMemShares[$ArrayIndex] $ArrayIndex += 1 } Clear-Variable CalcPoolMemShares, CalcPoolCPUShares, MaxCPUShares, MaxMemShares, CPUShareMultiplier, MemShareMultiplier, ArrayIndex, NumvCPUs, NumVMs, TotalMemory, i, pool #Next Cluster. } disconnect-VIServer -Server $vCenterServer -Force -confirm:$false