Piping a user's selection from a list is failing

Welcome Forums General PowerShell Q&A Piping a user's selection from a list is failing

This topic contains 2 replies, has 2 voices, and was last updated by

5 years, 6 months ago.

  • Author
  • #10406

    Points: 0
    Rank: Member

    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") 
    $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
    $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"
    $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"
    $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:"
    $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.Topmost = $True
    [void] $objForm.ShowDialog()

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

    I thought this line:


    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' 🙂

  • #10408

    Points: 0
    Rank: Member


    answered it myself...


    contains the value that is recorded so

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

    Points: 0
    Rank: Member

    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:"

        if ($_.KeyCode -eq "Enter") 
            $script:x = $objListBox.SelectedItem

The topic ‘Piping a user's selection from a list is failing’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort