I currently have a script (below) which equally creates resource pools across the number of hosts under a folder.
For example I have to deploy 6 Resource Pools and I have 3 hosts. The script will create 2 Resource Pools per host.
However what I would like is the script to find the host with the least number of resource pools and create the new resource pool on that host. And do this for the rest of the new resource pools.
SCRIPT
#
# Parameter Declarations
#
Param($folder_param, $envID_param, $network_param, $instructor_param, $region_param)
#
# Usage Statement
#
If (!$folder_param -or !$envID_param -or !$network_param -or !$instructor_param -or !$region_param)
{
"usage: BuildPools-Sandbox.ps1 -f folder -e envIdArray -n network"
Exit
}
#
# Retrieve Locations
#
$location = Get-Folder -Location "Production" -Name $folder_param
$vmhost_list = Get-VMHost -Location $location | Sort
If (!$location -or !$vmhost_list) { Exit }
#
# Main Script
#
If($vmhost_list.Length) {$numHost = $vmhost_list.Length} Else {$numHost = 1}
If($envID_param.Length) {$numEnv = $envID_param.Length} Else {$numEnv = 1}
$envPerHost = [Math]::Ceiling($numEnv/$numHost)
$pool_list = @()
$i = 0
Foreach ($vmhost in $vmhost_list)
{
""
$vmhost.Name.TrimEnd(".hpetrain.com")
"-------------"
For ($j=0; $j -lt $envPerHost; $j++)
{
if ($envID_param[$i*$envPerHost + $j])
{
$pool_name = "Env" + $envID_param[$i*$envPerHost + $j] + "_" + $network_param + "_Net" + ($j+1) + "_" + $instructor_param + "_" + $region_param
$pool_list = $pool_list + (New-ResourcePool -Location $vmhost -Name $pool_name -Confirm:$false)
" Creating: " + $pool_name
}
}
$i++
}
""
Would something like this do the trick ?
$numberOfPools = 6
1..$numberOfPools | %{
$tgtVmHost = Get-VMHost |
Get-ResourePool |
Group-Object -Property ParentId |
Sort-Object -Property {$_.Group.Count} |
Select -First 1 | %{$_.Group[0].Parent}
New-ResourcePool -Name "Pool$($_)" -Location $tgtVmHost
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks,
That does look like something that would work, but I' am struggling on how to implement that into my script.
I assume that you will get the number of resourcepools and the resourcepoolname-prefix as input ?
In that case the number shall replace the 6 in the 1..6 loop in the script.
And the prefix needs to be inserted into the name of the new resourcepool.
If I see that incorrectly, let me know what you have as input, and what the result should be
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes number of resourcepools is defined by envID_param. So user will input $a=(100..105) to create an array of IDs and -e=$a in the command argument. Resource pool name is also defined by parameters.
In my testing environment I have 3 hosts (Host A, B, C) with 14 resourcepools on each host.
So I changed my script as shown below. However when running the script all ResourcePools are created on Host B only. Output shown below.
SCRIPT
#
# Parameter Declarations
#
Param($folder_param, $envID_param, $network_param, $instructor_param, $region_param)
#
# Usage Statement
#
If (!$folder_param -or !$envID_param -or !$network_param -or !$instructor_param -or !$region_param)
{
"usage: BuildPools-Sandbox.ps1 -f folder -e envIdArray -n network -i instructor -r region"
Exit
}
#
# Retrieve Locations
#
$location = Get-Folder -Location "Production" -Name $folder_param
$vmhost_list = Get-VMHost -Location $location | Sort
If (!$location -or !$vmhost_list) { Exit }
#
# Main Script
#
$pool_list = @()
$i = 0
$numberOfPools = $envID_param.count
1..$numberOfPools | %{
$tgtVmHost = Get-VMHost -Location $location |
Get-ResourcePool | Group-Object -Property ParentId |
Sort-Object -Property {$_.Group.Count} |
Select -First 1 | %{$_.Group[0].Parent}
$pool_name = "Env" + $envID_param[$i++] + "_" + $network_param + "_Net" + ($j+1) + "_" + $instructor_param + "_" + $region_param
$pool_list = $pool_list + (New-ResourcePool -Location $tgtVmHost -Name $pool_name -Confirm:$false)
" Creating: " + $pool_name + "on" + $tgtVmHost.Name.TrimEnd(".hpetrain.com")
}
SCRIPT OUTPUT
PowerCLI C:\Scripts\Dev> $a = (100..105)
PowerCLI C:\Scripts\Dev> .\devTestEnvironmentBuild.ps1 -f 'hosts_folder' -e $a -n 77 -i Han_Solo -r Geneva
Creating: Env100_77_Net1_Han_Solo_Geneva on u04peeducos050
Creating: Env101_77_Net1_Han_Solo_Geneva on u04peeducos050
Creating: Env102_77_Net1_Han_Solo_Geneva on u04peeducos050
Creating: Env103_77_Net1_Han_Solo_Geneva on u04peeducos050
Creating: Env104_77_Net1_Han_Solo_Geneva on u04peeducos050
Creating: Env105_77_Net1_Han_Solo_Geneva on u04peeducos050
PowerCLI C:\Scripts\Dev>
Can you cecck if the following returns anything ?
Replace the hostname with the name of one your ESXi hosts.
Get-VMHost -Name MyEsx | Get-ResourcePool
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, it returns all resource pool twice and 2 resource pools called 'Resources', which are hidden in the vSphere client.
So it returns 30 resource pools instead of 14 that are actually visible in vSphere.
Twice ?
Are you connected multiple times ? What is in $global:defaultviservers ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Ah yes I had 2 connections.
I have one session running now and i'am seeing 14 resourcepools plus hidden 'Resources' after running Get-VMHost -Name MyEsx | Get-ResourcePool.
I ran the script again, however it is still creating the resourcepools on one host.
Seems I made a typo, replace line
Get-ResourcePool | Group-Object -Property ParentId |
by this line
Get-ResourcePool | Group-Object -Property Parent |
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I replaced line
Get-ResourcePool | Group-Object -Property ParentId |
with
Get-ResourcePool | Group-Object -Property Parent |
However still creating all resourcepools on Host B.
If I run the script below and echo the results $tgtVmHost
$tgtVmHost = Get-VMHost |
Get-ResourePool |
Group-Object -Property Parent |
Sort-Object -Property {$_.Group.Count}
I get the output below.
Count Name Group
----- ---- -----
1 u04peeducos050.hpetrai... {Resources}
1 u04peeducos051.hpetrai... {Resources}
1 u04peeducos049.hpetrai... {Resources}
39 Resources {Env178_79_Net2, Env179_79_Net3, Env182_79_Net6, Env186_79_Net10...}
As you can see the script only counts one ResourcePool per Host which is the hidden 'ResourcesPool' and then groups the rest of the resourcepools on a host called 'Resources' which doesn't exist.