VMware Cloud Community
RahmanK
Contributor
Contributor

PowerCLI increment Resource Pool name from existing pools on host

The script below creates resource pools across hosts and increments "_Net" when naming the Resource Pools.

However I would like the script to check existing resource pool "_Net" on the host and increment from there on.

For example on Host1 I have these existing Resource Pools:-

Env100_77_Net1_Jon_Snow_London

Env102_77_Net2_Jon_Snow_London

Env103_77_Net3_Jon_Snow_London

Env104_77_Net4_Jon_Snow_London


When running the script below, the "_Net" will start from 1 again when naming the Resource Pools however I would like it to start from 5.

I' am stuck on how to edit the below script to reach my goal and would appreciate some help.



#

# 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(".company.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++

}

""

Reply
0 Kudos
4 Replies
Zsoldier
Expert
Expert

Try this:

if ($envID_param[$i*$envPerHost + $j])

        {

            $pool_name = "Env" + $envID_param[$i*$envPerHost + $j] + "_" + $network_param + "_Net" + ($j+1) + "_" + $instructor_param + "_" + $region_param

               $netnum = $j+1

               While ($pool_name -eq (Get-ResourcePool $pool_name).name)

               {

               $netnum++

               $pool_name = "Env" + $envID_param[$i*$envPerHost + $j] + "_" + $network_param + "_Net" + ($netnum) + "_" + $instructor_param + "_" + $region_param

               }

          $pool_list = $pool_list + (New-ResourcePool -Location $vmhost -Name $pool_name -Confirm:$false)

          

            "  Creating:  " + $pool_name

        }

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
Reply
0 Kudos
RahmanK
Contributor
Contributor

Hi Zsoldier,

I edited my script with your suggestion.

I get the error below.

Get-ResourcePool : 5/11/2016 10:34:51 AM    Get-ResourcePool        ResourcePool with name 'Env600_

55_Net1_RobCook_HongKong' was not found using the specified filter(s).

At C:\Scripts\Dev\increment_resource_pool.ps1:49 char:55

+                While ($pool_name -eq (Get-ResourcePool <<<<  $pool_name).name)

    + CategoryInfo          : ObjectNotFound: (:) [Get-ResourcePool], VimException

    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmd

   lets.Commands.GetResourcePool

  Creating:  Env600_55_Net1_RobCook_HongKong

Updated Example:

Env102_77_Net1_Jon_Snow_London

Env102_77_Net2_Jon_Snow_London

Env103_77_Net3_Jon_Snow_London

Env104_77_Net4_Jon_Snow_London

Env600_55_Net5_RobCook_HongKong

Env601_55_Net6_RobCook_HongKong

Env602_55_Net7_RobCook_HongKong


So other variables in the resourcepool name can be different.

Reply
0 Kudos
Zsoldier
Expert
Expert

That error is normal because it won't find a resource pool w/ that name.

You want to just increment the _Net part regardless of the other parts of the name?

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
Reply
0 Kudos
RahmanK
Contributor
Contributor

Hi Zsoldier,

Yes regardless of the other parts of the resource pool name I want _Net to increment.

Thanks

Reply
0 Kudos