Getting output of invoke command into a combo box

DevSupport
DevSupport used Ask the Experts™
on
Hi Experts,

I am trying to get a servername from a text box and list its tomcat services as combo box items by putting it into array and displaying as dropdown.

I have created a button to fetch the items.

I can see that the invoke command works but does not populate into the Combo box.

$objappsvrname = New-Object System.Windows.Forms.TextBox
$objappsvrname.Location = New-Object System.Drawing.Size(20,45)
$objappsvrname.Size = New-Object System.Drawing.Size(260,15)
    $objForm.Controls.Add($objappsvrname)

$objloadbutton = New-Object System.Windows.Forms.Button
      $objloadbutton.Location = New-Object System.Drawing.Size(290,70)
      $objloadbutton.Size = New-Object System.Drawing.Size(75,20)
      $objloadbutton.Text = "Show TC"
      $objloadbutton.Add_Click({PopulateDBList2})
$objForm.Controls.Add($objloadbutton)




function PopulateDBList2{
$objAppDropDown = New-Object System.Windows.Forms.ComboBox
$objAppDropDown.Location = New-Object System.Drawing.Size(20,70)
$objAppDropDown.Size = New-Object System.Drawing.Size(260,15)
$servn = $objappsvrname.Text
write-host $servn
$ServiceNameArr = @()
$ServiceNameArr = (Invoke-Command -Computername $servn -ScriptBlock { Get-WMIObject Win32_Service -Filter "DisplayName LIKE '%Apache%'" | Select -ExpandProperty DisplayName })

ForEach ($item in $ServiceNameArr){
    $objAppDropDown.items.add($item.Name)
    write-host $item.Name
}
$objForm.Controls.Add($objAppDropDown)
}

Request you to please let me know what I am missing.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
The "item.Name" is incorrect. You're already expanding the DisplayName property, so you have a simple array of strings, and need to use the loop variable as it is.
ForEach ($item in $ServiceNameArr){
	$objAppDropDown.items.add($item)
	write-host $item
}

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial