下面的代码获取可用的数据中心名称并将它们添加到组合框。
function fill_dclist
{
$comboBox1.Items.Clear()
$dclist = Get-Datacenter
foreach ($dc in $dclist)
{
$comboBox1.Items.add($dc.name.toString())
}
下面的代码应该读取上面组合框中的选定项,并使用它来搜索该数据中心名称并显示该数据中心的所有虚拟机。
function fill_updatevmlist
{
$selecteddc = ($comboBox1.SelectedItem)
$dcvms = Get-datacenter -Name $selecteddc | get-VM
foreach ($dcvm in $dcvms)
{
[void]$listBox1.Items.Add($vm.name)
}
}
组合框代码:
###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
有高手吗?