Create Dropdown and pass selection back to script

jat0369
jat0369 used Ask the Experts™
on
I have a script that builds a CSV file (Name, Description).

I'd like to have some sort of pop-up occur that has a dropdown of only one of the collumns. Once the item is selected and the user clicks okay, it sends that variable back to the script for further processing.  I'm at a loss of how to go about doing this.   Would someone please help me out and show me a simple example of how to do this?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
I have written a function (http://www.ddls.net.au/blog/2012/05/powershell-select-an-item-from-a-list/) that takes a collection of objects, enable to use to select from either a text menu, list box or checked list box, either single or multiple selections, and the selected objects are output to the pipe for further processing.

NAME
Select-ItemFromList

SYNOPSIS
The Select-ItemFromList utility receives a collection of objects and will
display the list as either a numbered text list as a menu or in a list
box for selection. There is a switch to enable multiple item selection.

-------------------------- EXAMPLE 1 --------------------------
C:\PS>Select-ItemFromList -option "One","Two","Three" -mode Menu -selectionMode None
-------------------------- EXAMPLE 2 --------------------------
C:\PS>Select-ItemFromList -option "One","Two","Three" -mode Menu -selectionMode MultipleSimple
-------------------------- EXAMPLE 3 --------------------------
C:\PS>Select-ItemFromList -option (Get-Process) -displayProperty ProcessName -mode ListBox -selectionMode MultipleExtended
-------------------------- EXAMPLE 4 --------------------------
C:\PS>Select-ItemFromList -option (Get-Process) -displayProperty ProcessName -mode CheckedListBox

Author

Commented:
This is exactly what I'm looking for. Great work!
I took the SelectItemFromList.txt and renamed it dropdown.ps1
I referenced the file and called the function and It's giving me a bit of a problem. What am I doing wrong?

PS C:\Install\POS_PREP> $thisScript = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
PS C:\Install\POS_PREP> . ($thisScript + '.\dropdown.ps1')
PS C:\Install\POS_PREP> Select-ItemFromList -option "One","Two","Three" -mode Menu -selectionMode None
Unable to find type [System.Windows.Forms.SelectionMode]: make sure that the assembly containing this type is loaded.
At C:\Install\POS_PREP\dropdown.ps1:73 char:41
+     [System.Windows.Forms.SelectionMode] <<<< $selectionMode = [System.Windows.Forms.SelectionMode]::One
    + CategoryInfo          : InvalidOperation: (System.Windows.Forms.SelectionMode:String) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

PS C:\Install\POS_PREP>

Open in new window

Author

Commented:
Hi BChallis - any update to this? Any idea as to what I'm doing wrong?
Commented:
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 

$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "Select a Computer"
$objForm.Size = New-Object System.Drawing.Size(300,200) 
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
    {$x=$objListBox.SelectedItem;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
    {$objForm.Close()}})

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$x=$objListBox.SelectedItem;$objForm.Close()})
$objForm.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20) 
$objLabel.Size = New-Object System.Drawing.Size(280,20) 
$objLabel.Text = "Please select a computer:"
$objForm.Controls.Add($objLabel) 
$objListBox = New-Object System.Windows.Forms.ListBox 
$objListBox.Location = New-Object System.Drawing.Size(10,40) 
$objListBox.Size = New-Object System.Drawing.Size(260,20) 
$objListBox.Height = 80

 ForEach($i in (Get-Content Names.txt))
 {[void] $objListBox.Items.Add($i)}

$objForm.Controls.Add($objListBox) 
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})

[void] $objForm.ShowDialog()
$x

Open in new window

Author

Commented:
I ended up solving the issue myself using the code supplied. It's not as clean as the version bchallis supplied, but it works.

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