VMware Cloud Community
RahmanK
Contributor
Contributor

PowerCLI script Wait-Task error when deploying VMs

I have been writing an script to deploy VMs to resource pools specified in the command line.

However I have been getting a wait-task error saying 'a specified parameter is not correct'. Having a look through my script I can't see where a parameter is not specified correctly.

SCRIPT

#

# Parameter Declarations

#

Param($folder_param, $resourcePool_param, $environmentID_param)

#

# Function Definitions

#

Function Get-PoolID ($pool)

{

        $envID = ($pool.Name.Split("_"))[0].Substring(3)

        $networkID = ($pool.Name.Split("_"))[1].Substring(0)

        $netID = ( ($pool.Name.Split("_"))[2].Substring(3).Split(" ") )[0]

  $poolID = New-Object PSObject

        Add-Member -InputObject $poolID -MemberType NoteProperty -Name "Env" -Value $envID

        Add-Member -InputObject $poolID -MemberType NoteProperty -Name "Net" -Value $netID

        Add-Member -InputObject $poolID -MemberType NoteProperty -Name "Network" -Value $networkID

        $poolID

}  

#

# Usage Statement

#

If (!$folder_param -or !$resourcePool_param)

{

    "usage: CloneContent-Sandbox.ps1 -f targetFolder -r sourceResourcePool"

    Exit

}

#

# Retrieve Locations

#

$source = Get-Folder -Location 'Library' -Name 'Library Hosts' | Get-ResourcePool -Name $resourcePool_param

$sourceVM_list = Get-VM -Location $source | Sort

Write-Host -ForegroundColor "Yellow" ("Source found on " + ($sourceVM_list|Select -First 1).VMHost.Name.TrimEnd("hpetrain.com"))

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

$vmhost_list = Get-VMHost -Location $target | Sort

If (!$source -or !$sourceVM_list -or !$target -or !$vmhost_list) { Exit }

#

# Main Script

#

Foreach ($vmhost in $vmhost_list)

{

  $regexEnvId = $environmentID_param -join '|'

    $pool_list = Get-ResourcePool -Location $folder_param | Where { $_.Name -match $regexEnvId } | Sort

   

    Foreach ($pool in $pool_list)

    {       

        ""

  $pool.Name

        "-------------"

        $poolID = Get-PoolID $pool

       

  $process = @() # Added to consume return data from Wait-Task

        $tasks = @()

        Foreach ($sourceVM in $sourceVM_list)

        {

            $vm_name = $sourceVM.Name -replace "_bak.*",("_NET" + $poolID.Network + "_" + $poolID.Env)

            If ( !(Get-VM -Location $vmhost -Name $vm_name 2> $null) )

            {

  $tasks += (New-VM -VMHost $vmhost -ResourcePool $pool -VM $sourceVM -Name $vm_name -Confirm:$false -RunAsync)

            

                "  Cloning:  " + $vm_name

            }

        }

        If ($tasks)

        {

            # Wait-Task -Task $tasks

  $process = Wait-Task -Task $tasks

        }

    }

}

""

ERROR

Env100_77_Net14_Adam_Cable_Singapore

-------------

  Cloning:  AccessVM-SADMA_10.X_NET77_100

  Cloning:  SADMA_10.X_ms1_NET77_100

  Cloning:  SADMA_10.X_ms6_NET77_100

Wait-Task : 6/2/2016 7:14:52 AM    Wait-Task        The operation for the entity "AccessVM-SADMA_10.X_bak_2015-10-15" failed with the

following message: "A specified parameter was not correct.

"

At C:\Scripts\EnvironmentContentClone.ps1:78 char:24

+             $process = Wait-Task <<<<  -Task $tasks

    + CategoryInfo          : NotSpecified: (:) [Wait-Task], InvalidArgument

    + FullyQualifiedErrorId : Client20_TaskServiceImpl_CheckServerSideTaskUpdates_OperationFailed,VMware.VimAutomation.ViCore.Cmdlets

   .Commands.WaitTask

Wait-Task : 6/2/2016 7:14:53 AM    Wait-Task        The operation for the entity "SADMA_10.X_ms1_bak_2015-10-15" failed with the follo

wing message: "A specified parameter was not correct.

"

At C:\Scripts\EnvironmentContentClone.ps1:78 char:24

+             $process = Wait-Task <<<<  -Task $tasks

    + CategoryInfo          : NotSpecified: (:) [Wait-Task], InvalidArgument

    + FullyQualifiedErrorId : Client20_TaskServiceImpl_CheckServerSideTaskUpdates_OperationFailed,VMware.VimAutomation.ViCore.Cmdlets

   .Commands.WaitTask

Wait-Task : 6/2/2016 7:14:53 AM    Wait-Task        The operation for the entity "SADMA_10.X_ms6_bak_2015-10-15" failed with the follo

wing message: "A specified parameter was not correct.

"

At C:\Scripts\EnvironmentContentClone.ps1:78 char:24

+             $process = Wait-Task <<<<  -Task $tasks

    + CategoryInfo          : NotSpecified: (:) [Wait-Task], InvalidArgument

    + FullyQualifiedErrorId : Client20_TaskServiceImpl_CheckServerSideTaskUpdates_OperationFailed,VMware.VimAutomation.ViCore.Cmdlets

   .Commands.WaitTask

0 Kudos
1 Reply
RahmanK
Contributor
Contributor

Issue has now been resolved.

0 Kudos