Link to home
Start Free TrialLog in
Avatar of Fady AbuZuaiter
Fady AbuZuaiterFlag for Qatar

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Douglas Suyemoto
Douglas Suyemoto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial