从与另一个获取过程一起使用的组合框中选择的项目

时间:2013-01-10 17:53:22

标签: powershell powercli vsphere

下面的代码抓取可用的数据中心名称,并将它们添加到组合框中。

function fill_dclist 
{
$comboBox1.Items.Clear()
$dclist = Get-Datacenter
foreach ($dc in $dclist)
{
$comboBox1.Items.add($dc.name.toString())
}

以下代码应该读取上面组合框中的选定项目并使用它来搜索该数据中心名称并显示该数据中心的所有VM。

function fill_updatevmlist 
{
$selecteddc = ($comboBox1.SelectedItem)
$dcvms = Get-datacenter -Name $selecteddc | get-VM 
foreach ($dcvm in $dcvms)
{
[void]$listBox1.Items.Add($vm.name)
}
}

Combobox代码:

###DROPDOWN DC SELECT LIST###
$comboBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$comboBox1.FormattingEnabled = $True
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 7
$System_Drawing_Point.Y = 7
$comboBox1.Location = $System_Drawing_Point
$comboBox1.Name = "comboBox1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 21
$System_Drawing_Size.Width = 121
$comboBox1.Size = $System_Drawing_Size
$comboBox1.TabIndex = 0
$comboBox1.add_Click({

fill_dclist
fill_updatevmlist

})

目前收到以下错误

Get-Datacenter : Cannot validate argument on parameter 'Name'. The argument is null or empty. Supply an argument th
at is not null or empty and then try the command again.
At C:\Users\Olly\Documents\Dropbox\_PROJECT\First Stage\Code\draft.ps1:251 char:30
+ $dcvms = Get-datacenter -Name <<<<  $selecteddc | get-VM 
    + CategoryInfo          : InvalidData: (:) [Get-Datacenter], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetDat 
   acenter

Exception calling "Add" with "1" argument(s): "Value cannot be null.
Parameter name: item"
At C:\Users\Olly\Documents\Dropbox\_PROJECT\First Stage\Code\draft.ps1:254 char:26
+ [void]$listBox1.Items.Add <<<< ($vm.name)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

那里有专家吗?

1 个答案:

答案 0 :(得分:0)

上次,我的测试对象在这里,因为我没有vspehere。 :)尝试:

function fill_updatevmlist 
{
    $selecteddc = ($comboBox1.SelectedItem.toString())
    $dcvms = Get-datacenter -Name $selecteddc | get-VM 
    foreach ($dcvm in $dcvms)
    {
        [void]$listBox1.Items.Add($dcvm.name)
    }
}

你在foreach循环中输错了。我不确定第一个错误。这可能是因为您在组合框中选择任何内容之前尝试更新列表框。因此,如果上述方法无效,请尝试将其包括在内:

function fill_dclist 
{
    $comboBox1.Items.Clear()
    $dclist = Get-Datacenter
    foreach ($dc in $dclist)
    {
        $comboBox1.Items.add($dc.name.toString())
    }
}

删除你做的组合框add_click。组合框不是按钮。确保仅在表单加载时调用fill_dclist(如果您有刷新数据中心按钮,也是如此)。但是,不要在加载时调用fill_updatevmlist。只能在combobox1的selectedIndexChanged事件上调用fill_updatevmlist。您可以使用ex:$comboBox1.add_SelectedIndexChanged({fill_updatevmlist})进行分配。如果你确定只在组合框有一个新值后调用updatevmlist,它应该可以工作。

相关问题