Automation

 View Only
  • 1.  PowerCLI error when creating new resource pools

    Posted May 27, 2016 02:34 PM

    I' am currently writing a script which loops through a group of hosts and creates new resource pool on the host with the least resource pool count.

    However I receive the error below when the script is attempting to create a new resource pool on the target host.

    ======================================================================================================================

    ERROR

    New-ResourcePool : Cannot bind parameter 'Location'. Cannot convert the "" value of type "System.Management.Automation.P

    SCustomObject" to type "VMware.VimAutomation.ViCore.Types.V1.Inventory.VIContainer".

    At C:\Scripts\Dev\devHashBuildEnvironment.ps1:51 char:66

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

        + CategoryInfo          : InvalidArgument: (:) [New-ResourcePool], ParameterBindingException

        + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewResourcePool

    ======================================================================================================================

    ======================================================================================================================

    SCRIPT

    Param($folder_param, $envID_param, $network_param, $instructor_param, $region_param)

    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

    }

    $location = Get-Folder -Location "Production" -Name $folder_param

    $vmhost_list = Get-VMHost -Location $location | Where {$_.Name -ne 'Resources'} | Sort

    If (!$location -or !$vmhost_list) { Exit }

    $pool_list = @()

    $i = 0

    $numberOfPools = $envID_param.count

    $hosts = @{}

    1..$numberOfPools | %{

      Foreach ($vmhost in $vmhost_list){

      $hostPools = Get-ResourcePool -Location $vmhost

      $noOfPools = $hostPools.count

      $hosts = $hosts + @{$vmhost = $noOfPools}

      }

      $tgtVmHost = $hosts.GetEnumerator() | Sort Value | Select -First 1 | Select Name

              $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

      $hosts.clear()

      }

    ======================================================================================================================


    If i run echo $tgtVmHost the output shows the correct host.


    Name

    ===============

    hostA.domain.com



  • 2.  RE: PowerCLI error when creating new resource pools

    Posted May 27, 2016 05:16 PM

    Change the line to the following.

    That way you only have the name as a [string], and OBN should be able to resolve the Location

    $tgtVmHost = $hosts.GetEnumerator() | Sort Value | Select -First 1 | Select -ExpandProperty Name



  • 3.  RE: PowerCLI error when creating new resource pools

    Posted May 31, 2016 09:30 AM

    Thanks that did the trick.