Author Posts

September 27, 2013 at 5:52 am

I am trying to understand the code used to create a form from which the user can select an item. For instance...the form lists computernames and when the user selects a computername from the list, that computername is piped to a powershell script.

I found an excellent example of how to create the form on the Tech Net site(see code below) but I am unsure how to pipe the selected item to a script.

[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

[void] $objListBox.Items.Add("atl-dc-001")
[void] $objListBox.Items.Add("atl-dc-002")
[void] $objListBox.Items.Add("atl-dc-003")
[void] $objListBox.Items.Add("atl-dc-004")
[void] $objListBox.Items.Add("atl-dc-005")
[void] $objListBox.Items.Add("atl-dc-006")
[void] $objListBox.Items.Add("atl-dc-007")

$objForm.Controls.Add($objListBox) 

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

$x

Running the script and then selecting any of the available computers does not result in $x storing any value...

I thought this line:

$OKButton.Add_Click({$x=$objListBox.SelectedItem;$objForm.Close()})

was used to store the computer I selected when I click the computername then click OK, but $x does not contain any value.

How can I actually record my selection from the list and pass that to the rest of my script?

P.S. A "Preview Post" function would be awesome...just sayin' 🙂

September 27, 2013 at 7:26 am

nm...

answered it myself...

$objListBox.SelectedItem

contains the value that is recorded so

$x=($objListBox.SelectedItem),/pre> puts the value in X

September 27, 2013 at 8:57 am

It's probably cleaner to check $objListBox.SelectedItem after the call to $objForm.ShowDialog() , but if you really wanted to do that from inside the KeyDown / Click handlers, you'd have to scope the variable, like this: (code reformatted for clarity, but all I added was "script:"

$objForm.Add_KeyDown({
    if ($_.KeyCode -eq "Enter") 
    {
        $script:x = $objListBox.SelectedItem
        $objForm.Close()
    }
})