«

»

Mar 20 2013

vSphere cluster resource pool configuration script

“VMTurbo"
This blog will include the technical aspect (the script) which i mentioned in the blog post vCloud Director implementation for small businesses

Input parameters:

  • 4 vSphere cluster resource pools exists at the vSphere cluster root level
    • SLA-Gold with priority value 10
    • SLA-Silver with priority value 5
    • SLA-Bronze with priority value 3
    • SLA-vCloud with priority value 1

    Screen Shot 2013-03-20 at 21.22.30

  • Each of the vSphere cluster resource pool includes 4 virtual machines:
    • SLA-X-VM-01, 2 vCPU, 1 GB RAM
    • SLA-X-VM-02, 2 vCPU, 1 GB RAM
    • SLA-X-VM-03, 2 vCPU, 1 GB RAM
    • SLA-X-VM-04, 2 vCPU, 1 GB RAM

The below figure shows the vSphere cluster resource pool shares configuration in the vSphere Web Client before i ran the script.
Screen Shot 2013-03-20 at 14.50.22
When running the PowerCLI configuration script i receive the output presented in the below figure.
Screen Shot 2013-03-20 at 14.48.14
The below figure shows the vSphere cluster resource pool configuration in the vSphere Web Client after running the script the first time.
Screen Shot 2013-03-20 at 14.49.15
The Shares values seems to be ok if using the calculation formula described in the blog post http://vcdx56.com/2013/03/19/vcloud-director-implementation-for-small-businesses/

  • CPU Shares per vSphere cluster resource pool = “SLA category priority value” X “number of total vCPUs assigned to the virtual machines in the vSphere cluster resource pool” 
  • Memory Shares per vSphere cluster resource pool = “SLA category priority value” X “number of total Memory GB assigned to the virtual machines in the vSphere cluster resource pool”

Using the above formula we get the following figures for the vSphere cluster resource pool SLA-Gold:

  • CPU Shares
    • 10 (SLA priority value) X 8 (number of vCPUs in the vSphere cluster resource pool) = 80
  • Memory Shares
    • 10(SLA priority value) X 4 (number of Memory GB in the vSphere cluster resource pool) = 40

I increased the number of vCPUs and number of Memory GB for the virtual machine SLA-vCloud-VM-01 according to the below figure.
Screen Shot 2013-03-20 at 14.54.46
The below figure shows the PowerCLI script output after running the script the second time. The values, for the SLA-vCloud section, changed compared to the first PowerCLI script output figure.
2
The Shares configuration changed to the expected values for the SLA-vCloud vSphere cluster resource pool according to the following calculation:

  • CPU Shares
    • 1 (SLA priority value) X 22 (number of vCPUs in the vSphere cluster resource pool) = 22
  • Memory Shares
    • 1 (SLA priority value) X 19 (number of Memory GB in the vSphere cluster resource pool) = 19

The below figure shows the vSphere cluster resource pool configuration in the vSphere Web Client after running the script the second time.
Screen Shot 2013-03-20 at 14.52.40
Below you’ll find the script and again thanks to Luc Dekens @LucD22 , Alan Renouf @alanrenouf  and Niklas Åkerlund @vNiklas for your input while creating the script.

The “Get login password” section in the script is used to avoid typing the password, required for the user running the script, in plain text in the script or in another file. The password is stored, encrypted, in the powerclicred file and can be used for vCenter Server authentication when scheduling the script in the “Windows Task Scheduler”.

Change the red marked text in the script to your required values.

# Script to configure vSphere Cluster Resource Pools shares based SLA priority and number of powered on VMs per vSphere Cluster Resource Pool
#
# Version 1.0
# Magnus Andersson, Real Time Services AB
#
Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue
#
#
$RPShares = @{}
#___________________________________________________________
Manual configuration section starts here
#
#Change the “name of the vSphere cluster resource pool” to the name of the vSphere Cluster Resource Pool you need to include.
# Change the X value. This value reflects the shares value per vCPU and GB RAM and should be the pre-defined SLA based priority value.
# Create one line per vSphere Cluster Resource Pool you need to include.
#
$RPShares.Add(“name of the vSphere cluster resource pool“, X)
#
#
# Get login password
$pwd = Get-Content c:vspherescriptspowerclicred | ConvertTo-SecureString
$cred = New-Object System.Management.Automation.PsCredential “domainusername“, $pwd
#
#
# Connect to vCenter Server
#
connect-viserver vCenterServer
# Manual configuration section ends here
#___________________________________________________________
#
#
foreach ($cluster in (get-cluster)) {
foreach ($p in ($RPShares.Keys | Sort-Object)) {
$RP = $cluster | Get-ResourcePool | ?{$_.Name -like $p}
$VMs = $RP | Get-VM | ?{$_.PowerState -eq “PoweredOn”}
$TotVRAM = (($vms | select MemoryMb | Measure-Object -Property MemoryMb -Sum).Sum/1000)
$TotVCPU = (($vms | select NumCPU | Measure-Object -Property NumCpu -Sum).Sum/1)
$VRAMShares = [math]::round($TotVRAM * $RPShares.Item($p))
$VCPUShares = [math]::round($TotVCPU * $RPShares.Item($p))
#
Write-Host “Resource Pool:”$RP.Name
Write-Host “Total CPU Shares: $VCPUShares (Total $TotVCPU vCPUs)”
Write-Host “Total RAM Shares: $VRAMShares (Total $TotVRAM GB RAM)”
Write-Host “”
#
if ($TotVCPU) {$RP | Set-ResourcePool -NumCpuShares $VCPUShares -Confirm:$false | Out-Null}
else {$RP | Set-ResourcePool -NumCpuShares 0 -Confirm:$false | Out-Null}
if ($TotVRAM) {$RP | Set-ResourcePool -NumMemShares $VRAMShares -Confirm:$false | Out-Null}
else {$RP | Set-ResourcePool -NumMemShares 0 -Confirm:$false | Out-Null}
}
}

I created a blog post about how to schedule a PowerCLI script in Windows task scheduler and it can be found here

8 comments

2 pings

Skip to comment form

  1. pozycjonowanie

    Great site! I am loving it!! Will come back again. I am taking your feeds also

  2. magander3

    Thanks, glad you like the site /Magnus

  3. Unknown

    I constantly spent my half an hour to read this web site’s articles everyday along with a mug of coffee.

  4. unknown

    I do not even know how I ended up here, but I thought this post was great.
    I don’t know who you are but definitely you’re going to a famous blogger if you are not already 😉 Cheers!

  5. Bauchfett verlieren

    Thanks for finally talking about >vSphere cluster resource pool configuration script | vcdx56 <Loved it!

  6. magander3

    Thank you, glad you liked it / Magnus

  7. ohenrard

    Hi, thank you for this script ! I tried it and get errors (it calculates the resouce pools i listed, but it seems to look at datastores too). Any idea ?
    Thanks again ! Olivier

    Get-VM : Impossible de valider l’argument sur le paramètre « Datastore ». L’argument est null ou vide. Indiquez un argu
    ment qui n’est pas null ou vide et réessayez.
    Au niveau de D:ResourcePoolCalculation.ps1 : 38 Caractère : 20
    + $VMs = $RP | Get-VM <<<< | ?{$_.PowerState -eq "PoweredOn"}
    + CategoryInfo : InvalidData: (:) [Get-VM], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM

    Resource Pool:
    Total CPU Shares: 0 (Total 0 vCPUs)
    Total RAM Shares: 0 (Total 0 GB RAM)

  8. magander3

    Hi,
    i really can’t understand the error. Can you send me the output in english using the contact form via the author page on my blog? Include the entire script and a print screen of the venter environment if possible.

    //Magnus

  1. Schedule PowerCLI script in Windows task scheduler | vcdx56

    […] about how to add a PowerCLI scrip to Windows task scheduler after publishing my blog post vSphere Cluster Resource Pool Configuration Script Depending on the customer requirements there are several ways of doing this. Customer requirements […]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">