PowerShell - Populate my listbox with a selection made in drop down list

286 Views Asked by At

I am trying to make an Application Deployment tool on PowerShell ISE by pipelining the data through Active Directory for admins. Since Active Directory doesn't have a feature of fuzzy search, I thought that I would create a GUI application to help the admin to search, add and deploy the application. I decided to use Drop down list (ddl) to show the applications available, since ddl provides a text input as well. Then whatever application is selected from the ddl it should be added to a list box beneath. That listbox has multiselect option where a user can multiselect and delete the selected items. Unfortunately I am stuck at a place where I am trying to populate my Listbox through the selection made in the drop down list.

Since extracting data from AD was not a difficult part but passing that selected data from the ddl to listbox is just not happening. I tried using a .Add_click to pass the information to the listbox. Neither the selected app is going through nor I am able to make any selection in that list box. I will provide snippets of my code. I

Add-Type -AssemblyName System.Windows.Forms
import-module ActiveDirectory

#Intro

Write-host "`n`This Script is an Application Deployer.`n`r"

Add-Type -AssemblyName System.Windows.Forms
$FormObject = [System.Windows.Forms.Form]
$LabelObject = [System.Windows.Forms.Label]
$ComboBoxObject= [System.Windows.Forms.ComboBox]
$ListBox = [System.Windows.Forms.Listbox]


#Main Window
$AppForm = New-Object $FormObject 
$AppForm.ClientSize = '1500,1000'
$AppForm.Text = 'Search and Deploy Application'
$AppForm.BackColor = 'White'
$AppForm.Font = $DefaultFont

$AppLabel = New-Object $LabelObject
$AppLabel.Text = 'Applications :'
$AppLabel.AutoSize = $true 
$AppLabel.Location = New-Object System.Drawing.Point (100,202)


$StdAppLabel = New-Object $LabelObject
$StdAppLabel.Text = 'Selected Applications : '
$StdAppLabel.AutoSize = $true 
$StdAppLabel.Location = New-Object System.Drawing.Point (100,262)

#Selected App Name
$StdAppName = New-Object $Labelobject
$StdAppName.Text= 'Text'
$StdAppName.AutoSize=$true
$StdAppName.Location=New-Object System.Drawing.Point (225,262)

#Application DropDown 
$appddl = New-Object $ComboBoxObject
$appddl.width = '520'
$appddl.Font = 'Verdana, 10'
$appddl.Location = New-Object System.Drawing.Point(180,195)

function Load-Combobox-Apps
{
    $appddl.Items.Clear()
    
    $apparray = Get-ADGroup -Filter {name -like "AP_SCCM_FBC_*"} | Select-Object Name, @{Name= 'Appname' ; Expression = {$_.Name -split 'AP_SCCM_FBC_' -join ''}} 
    
    ForEach ($Item in $apparray)
    {
        $appddl.Items.Add($item.Appname)
        $appddl.AutoCompleteSource = 'listItems'
        $appddl.AutoCompleteMode = 'Suggest'
      
    }
}

Load-Combobox-Apps

    
function GetApplicationDetails
{

    $ApplistBox = New-Object System.Windows.Forms.ListBox
    $ApplistBox.Location = New-Object System.Drawing.Point(222,257)
    $ApplistBox.width = 480
    $ApplistBox.SelectionMode = 'MultiExtended'
    $ApplistBox.Height = 300


  #  $AppName=$appddl.SelectedItem
  #  $details= Get-ADGroup $AppName | select *
  #  $StdAppName.Text=$details.name

    $ApplistBox.Add_Click{
    $SelectedItems | ForEach-Object {$ApplistBox.Items.Add($_appddl.Appname)}
        return $SelectedItems
}

    #$FormObject.Controls.Add($appddl)

    $AppForm.Controls.Add($ApplistBox)

    $AppForm.Add_Shown({ApplistBox.Select})

} 


$appddl.Add_SelectedIndexchanged({GetApplicationDetails})


$AppForm.Controls.AddRange(@( $AppLabel,$appddl, $StdAppLabel,  $StdAppName))


$AppForm.ShowDialog()

$AppForm.Dispose()
1

There are 1 best solutions below

0
Scepticalist On

I'd suggest that you use one list with checkboxes instead, but if you really want to do it this way:

Add-Type -AssemblyName System.Windows.Forms
Import-Module ActiveDirectory

#Intro

Write-host "`n`This Script is an Application Deployer.`n`r"

Add-Type -AssemblyName System.Windows.Forms
$FormObject = [System.Windows.Forms.Form]
$LabelObject = [System.Windows.Forms.Label]
$ComboBoxObject= [System.Windows.Forms.ComboBox]
$ListBox = [System.Windows.Forms.Listbox]


#Main Window
$AppForm = New-Object $FormObject 
$AppForm.ClientSize = '1500,1000'
$AppForm.Text = 'Search and Deploy Application'
$AppForm.BackColor = 'White'
$AppForm.Font = $DefaultFont

$AppLabel = New-Object $LabelObject
$AppLabel.Text = 'Applications :'
$AppLabel.AutoSize = $true 
$AppLabel.Location = '100,202'


$StdAppLabel = New-Object $LabelObject
$StdAppLabel.Text = 'Selected Applications : '
$StdAppLabel.AutoSize = $true 
$StdAppLabel.Location = '100,262'

#Selected App Name
$StdAppName = New-Object $Labelobject
$StdAppName.Text= 'Text'
$StdAppName.AutoSize=$true
$StdAppName.Location= '125,242'

#Application DropDown 
$appddl = New-Object $ComboBoxObject
$appddl.width = '520'
$appddl.Font = 'Verdana, 10'
$appddl.Location = '180,195'


$ApplistBox = New-Object System.Windows.Forms.ListBox
$ApplistBox.Location = '222,257'
$ApplistBox.Size = '480,300'
$ApplistBox.SelectionMode = 'MultiExtended'

$AppForm.Controls.AddRange(@( $AppLabel,$appddl,$StdAppLabel,$StdAppName,$ApplistBox))

function Load-Combobox-Apps {
    $appddl.Items.Clear()
    $apparray = $appDetails | Select-Object Name, @{Name= 'Appname' ; Expression = {$__.Name -split 'AP_SCCM_FBC_' -join ''}} 
    
    ForEach ($Item in $apparray) {
        $appddl.Items.Add($item.Appname) | Out-Null
        $appddl.AutoCompleteSource = 'listItems'
        $appddl.AutoCompleteMode = 'Suggest'
    }
}


# Actions to carry out when new item selected from dropdown
$appddl.Add_SelectedIndexchanged({
    $AppName = $appddl.SelectedItem
    $ApplistBox.Items.Add($AppName) | Out-Null
    # Create list of objects matching selected apps for further use
    $Script:CurrentlySelectedApps = $appdetails | Where-Object { ($__.Name -split 'AP_SCCM_FBC_' -join '') -in $ApplistBox.Items }
})

$AppForm.Add_Shown({
    # Get Group details once, so that it can be re-used
    $appDetails= Get-ADGroup -Filter {name -like "AP_SCCM_FBC_*"}
    Load-Combobox-Apps
})

$AppForm.ShowDialog()
$AppForm.Dispose()