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.