Question

Below code grabs the available datacenter names and adds them to a combobox ok.

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

the below code should read the selected item in the above combobox and use this to search against that datacenter name and display all VMs from that datacenter.

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

Combobox code:

###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

})

Currently getting the following errors

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

Any gurus out there?

Was it helpful?

Solution

As last time, your my testsubject here since I don't have vspehere. :) Try:

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

You had typo in foreach loop. I'm unsure about the first error. It might be because you try to update the listbox before anything is selected in combobox. So if the above does not work, try to include this too:

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

Remove that combobox add_click thing your doing. combobox is not a button. Make sure you call fill_dclist ONLY on form load(also if you have a refresh datacenter button). HOWEVER, don't call fill_updatevmlist on load. ONLY call fill_updatevmlist on combobox1's selectedIndexChanged event. You assign this using ex: $comboBox1.add_SelectedIndexChanged({fill_updatevmlist}) . If you make sure updatevmlist is only called after the combobox has a NEW value, it should work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top