-1

以这段代码为例,该函数使用 vsphere cmd-let 根据列表框中选定的 VM 检索 CPU 编号。输出正在另一个列表框中工作,但我希望能够将结果输出到标签

function fill_updatelistbox 
{
$vmlist1 = Get-vm -Name $listBox1.SelectedItem | select NumCPU
foreach ($vm1 in $vmlist1)
{
$label1.Text($vm1.NumCPU)
}
}


$label1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 400
$System_Drawing_Point.Y = 115
$label1.Location = $System_Drawing_Point
$label1.Name = "label1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 100
$label1.Size = $System_Drawing_Size
$label1.TabIndex = 0
$label1.Text = ""

编辑:下面的代码使用列表框工作

function fill_updatelistbox
{
$vmlist1 = Get-vm -Name $listBox1.SelectedItem | select NumCPU
foreach ($vm1 in $vmlist1)
{
[void]$listBox2.Items.Add($vm1.NumCPU)
}
}

$tabPage2.Controls.Add($panel2)
$listBox2.DataBindings.DefaultDataSourceUpdateMode = 0
$listBox2.FormattingEnabled = $True
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 1
$System_Drawing_Point.Y = 1
$listBox2.Location = $System_Drawing_Point
$listBox2.Name = "listBox2"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 369
$System_Drawing_Size.Width = 160
$listBox2.Size = $System_Drawing_Size
$listBox2.TabIndex = 0
$panel2.Controls.Add
4

1 回答 1

0

仅供参考,我没有资源来测试这个,但我会尽力提供帮助。首先,确保列表框是单选的,因为标签更像是一个单项文本框:) 然后你可以尝试:

function fill_updatelistbox 
{
#Should return a single VirtualMachine object
$vm = Get-vm -Name ($listBox1.SelectedItem.toString())
$label1.Text = $vm.NumCPU.toString()
}
于 2013-01-09T18:25:07.320 回答