从.csv文件部署VM时,Wait-Task输出错误

问题描述

我编写了一个GUI脚本,该脚本从模板克隆VM,并为它们提供从.csv文件获得的静态IP地址。

除我遇到的错误外,所有内容似乎都可以正常工作。无论如何,克隆成功完成,但是我不确定如何解决错误,甚至不确定。

function StartClone {
    $VM_List = Import-Csv $csvTB.Text
    $numClones = [int]((Get-Content $csvTB.Text).Length)
    $vmh = Get-VMHost
    $NewParameters = @{
        # Name                = ''
        Template            = $TemplateMenu.Text
        Datastore           = $DatastoreMenu.Text
        diskStorageFormat   = 'Thin'
        Location            = $FolderCB.Text
        OSCustomizationSpec = $CustomizationCB.Text
        VMHost              = Get-Random -InputObject $vmh
        Server              = $VCenterTB.Text
        RunAsync            = $true
    }

    $SetParameters = @{
        Numcpu              = $cpu_TB.Text
        MemoryGB            = $RAM_TB.Text
        Notes               = $NotesTB.Text
        Confirm             = $false
    }

    $taskList = if ($NumClones -gt 0) {
        # foreach ($item in (Import-Csv $csvTB.Text))
        $VM_List | ForEach-Object {
            $NewParameters['Name'] = "$($_.Hostname)"
            Get-OSCustomizationSpec -name $CustomizationCB.Text | Get-OSCustomizationNICMapping | Set-OSCustomizationNICMapping -IPMode UseStaticIP -IPAddress "$($_.IP)" -subnetMask "$($_.subnet)" -DefaultGateway "$($_.Gateway)" -Dns ""
            New-VM @NewParameters
        }
    }

    $newVM = $taskList | Wait-Task -ErrorAction SilentlyContinue
    $newVM | Set-VM @SetParameters
    $newVM | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $VLAN_CB.Text -Confirm:$false
    if ($startVM_CB.Checked -eq $true) {$newVM | Start-VM }
 }

我得到的错误Wait-Task命令有关,我必须使用它来等待虚拟机完成克隆。

Wait-Task : The input object cannot be bound to any parameters for the command either because 
the command does not take pipeline input or the input and its properties do not match any of 
the parameters that take pipeline input.

At line:465 char:26
+     $newVM = $taskList | Wait-Task
+                          ~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (UseStaticIP:192.168.1.1:PSObject) [Wait-Tas     k],ParameterBindingException                                                                     + FullyQualifiedErrorId : InputObjectNotBound,VMware.VimAutomation.Common.Commands.Cmdlets.     WaitTask

对于我的.csv文件中的每个VM都会重复出现该错误,因此,如果我要部署4个VM,它将弹出4次。正如错误所暗示的,这与我遍历.csv文件并使用迭代创建新VM的事实有关,但是就像我说的那样,一切都已完成而没有问题,并且VM可以正常工作。

任何对此的投入都是很好的。谢谢。

解决方法

New-VM返回一个VirtualMachineImpl对象,而Wait-Task没有任何关系...

如果将-RunAsync参数添加到New-VM,它将返回一个TaskImpl对象,然后可以将结果通过管道传递到Wait-Task cmdlet。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...