ComboBox Selected item action with Powershel

Hello,

I created the below PowerShell Script with .Net to get some VM information from Hyper-V and apply some firewall actions.

i created a ComboBox ( Drop Down Menu) with the VMs on the host but i need to get the host name that hosted the VM and it is not working can some one help below the whole code.

from the Combo Box you can start from Line 40.

This is the Code for the Combobox
##################################
$LVMName1 = New-Object System.Windows.Forms.Label
$LVMName1.Location = New-Object System.Drawing.Point(10,20)
$LVMName1.Size = New-Object System.Drawing.Size(280,20)
$LVMName1.Text = '1- Choose The VM Name:'
$form.Controls.Add($LVMName1)
$LHostname1 = New-Object System.Windows.Forms.Label
$LHostname1.Location = New-Object System.Drawing.Point(330,20)
$LHostname1.Size = New-Object System.Drawing.Size(280,20)
$LHostname1.Text = 'Host Name:'

$LHostname2 = New-Object System.Windows.Forms.Label
$LHostname2.Location = New-Object System.Drawing.Point(330,40)
$LHostname2.Size = New-Object System.Drawing.Size(280,20)

$U_hosts  = @('DOHPP1C11HPRV01','DOHPP1C11HPRV02','DOHPP1C11HPRV03','DOHPP3C11HPRV04', 'DOHPP3C11HPRV05','DOHPP3C11HPRV06')
$TVMName1 = New-Object System.Windows.Forms.combobox
$TVMName1.Location = New-Object System.Drawing.Point(10,40)
$TVMName1.Size = New-Object System.Drawing.Size(260,20)

foreach ($U_host in $U_hosts) {
$VMM2=get-vm -ComputerName $u_host | Select name,ComputerName
$VMM3=$VMM2.Name
$VMM4=$VMM2.ComputerName
foreach ($NoOfVM in $VMM3 ) {
$VMM4=$VMM2.ComputerName
$TVMName1.Items.add("$NoOfVM")
}

}


$form.Controls.Add($LHostname1)
$form.Controls.Add($LHostname2)
$form.Controls.Add($TVMName1)
##################################

Open in new window


And below the Full Script and To run the script copy and past it on PowerShell ISE

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$form = New-Object System.Windows.Forms.Form
$form.Text = 'Hyper-v vFirewall'
$form.Size = New-Object System.Drawing.Size(600,400)
$form.StartPosition = 'CenterScreen'

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(75,320)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(150,320)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)

##################################
$LVMName1 = New-Object System.Windows.Forms.Label
$LVMName1.Location = New-Object System.Drawing.Point(10,20)
$LVMName1.Size = New-Object System.Drawing.Size(280,20)
$LVMName1.Text = '1- Choose The VM Name:'
$form.Controls.Add($LVMName1)
$LHostname1 = New-Object System.Windows.Forms.Label
$LHostname1.Location = New-Object System.Drawing.Point(330,20)
$LHostname1.Size = New-Object System.Drawing.Size(280,20)
$LHostname1.Text = 'Host Name:'

$LHostname2 = New-Object System.Windows.Forms.Label
$LHostname2.Location = New-Object System.Drawing.Point(330,40)
$LHostname2.Size = New-Object System.Drawing.Size(280,20)

$U_hosts  = @('DOHPP1C11HPRV01','DOHPP1C11HPRV02','DOHPP1C11HPRV03','DOHPP3C11HPRV04', 'DOHPP3C11HPRV05','DOHPP3C11HPRV06')
$TVMName1 = New-Object System.Windows.Forms.combobox
$TVMName1.Location = New-Object System.Drawing.Point(10,40)
$TVMName1.Size = New-Object System.Drawing.Size(260,20)

foreach ($U_host in $U_hosts) {
$VMM2=get-vm -ComputerName $u_host | Select name,ComputerName
$VMM3=$VMM2.Name
$VMM4=$VMM2.ComputerName
foreach ($NoOfVM in $VMM3 ) {
$VMM4=$VMM2.ComputerName
$TVMName1.Items.add("$NoOfVM")
}

}


$form.Controls.Add($LHostname1)
$form.Controls.Add($LHostname2)
$form.Controls.Add($TVMName1)
##################################



##################################

$LDirection1 = New-Object System.Windows.Forms.Label
$LDirection1.Location = New-Object System.Drawing.Point(10,80)
$LDirection1.Size = New-Object System.Drawing.Size(280,20)
$LDirection1.Text = '2- Inbound Rules Direction.'
$form.Controls.Add($LDirection1)

##################################

$LVMIP1 = New-Object System.Windows.Forms.Label
$LVMIP1.Location = New-Object System.Drawing.Point(330,80)
$LVMIP1.Size = New-Object System.Drawing.Size(280,20)
$LVMIP1.Text = 'VM IP:'
$form.Controls.Add($LVMIP1)

##################################
$LRemoteIP1 = New-Object System.Windows.Forms.Label
$LRemoteIP1.Location = New-Object System.Drawing.Point(10,110)
$LRemoteIP1.Size = New-Object System.Drawing.Size(280,20)
$LRemoteIP1.Text = '3- Please Enter Remote IP Address or Subnets:'
$form.Controls.Add($LRemoteIP1)

$TVMName13 = New-Object System.Windows.Forms.TextBox
$TVMName13.Location = New-Object System.Drawing.Point(10,130)
$TVMName13.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($TVMName13)

##################################

$LPorts = New-Object System.Windows.Forms.Label
$LPorts.Location = New-Object System.Drawing.Point(10,160)
$LPorts.Size = New-Object System.Drawing.Size(280,20)
$LPorts.Text = '4- Please Enter Required Ports:'
$form.Controls.Add($LPorts)

$TVMName14 = New-Object System.Windows.Forms.TextBox
$TVMName14.Location = New-Object System.Drawing.Point(10,180)
$TVMName14.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($TVMName14)

##################################

$LProtocol1 = New-Object System.Windows.Forms.Label
$LProtocol1.Location = New-Object System.Drawing.Point(10,210)
$LProtocol1.Size = New-Object System.Drawing.Size(280,20)
$LProtocol1.Text = '5- Choose Protocol Name:'
$form.Controls.Add($LProtocol1)

$Protocols1 = @("TCP","UDP")
$TVMName15 = New-Object System.Windows.Forms.Combobox
$TVMName15.Location = New-Object System.Drawing.Point(10,230)
$TVMName15.Size = New-Object System.Drawing.Size(260,20)
foreach($Protocols2 in $Protocols1)
{
  $TVMName15.Items.add($Protocols2)
}
$form.Controls.Add($TVMName15)

##################################

$LResults1 = New-Object System.Windows.Forms.Label
$LResults1.Location = New-Object System.Drawing.Point(10,270)
$LResults1.Size = New-Object System.Drawing.Size(280,20)
$LResults1.Text = 'Results:'
$form.Controls.Add($LResults1)

##################################

$form.Topmost = $true
$form.Add_Shown({$TVMName1.Select()})
$form.Add_Shown({$TVMName13.Select()})
$form.Add_Shown({$TVMName14.Select()})
$form.Add_Shown({$TVMName15.Select()})
$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    $x = $TVMName1.Text
    $z = $TVMName13.Text
    $i = $TVMName14.Text
    $f = $TVMName15.Text
    $x
    $y
    $z
    $i
    $f
    
}

Open in new window

LVL 2
fadyazAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dsuyemotoCommented:
You need another variable to store all the VM's:

foreach ($U_host in $U_hosts) {

$VMM2=get-vm -ComputerName $u_host | Select name,ComputerName
$VMM3=$VMM2.Name
$VMM4=$VMM2.ComputerName
foreach ($NoOfVM in $VMM3 ) {
$VMM4=$VMM2.ComputerName
$TVMName1.Items.add("$NoOfVM")
}
$allVMs  += $VMM2
}

Then you need an event handler to get the selected item and pass that back to get the object from the list:

$TVMName1.add_SelectedIndexChanged($TVMName1_SelectedIndexChanged)
$TVMName1_SelectedIndexChanged=
{
    $vmname = $TVMName1.Text
    $vmhost = $allVMs | ? { $_.Name -eq $vmname }
    $LHostname2.Text = $vmhost.ComputerName
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.