Need help with script capturing the results from selection box RRS feed

  • Question

  • I found a script online and changed the selection text box to a get command. The list shows from ADUC, I can select the OU and then click OK, the box will close, but I am unable to capture the results. There are 2 variables in the get command that are from another part of the script. Can someone tell me what I am missing.

    My goal is to have the selection write to a variable like $result = ------


    [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(400,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 an Organization:"

    $objListBox = New-Object System.Windows.Forms.ListBox
    $objListBox.Location = New-Object System.Drawing.Size(10,40)
    $objListBox.Size = New-Object System.Drawing.Size(375,200)
    $objListBox.Height = 80

    Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchBase $OUSub -SearchScope OneLevel | Select-Object -ExpandProperty name | ForEach-Object {[void] $objListBox.Items.Add($_)}


    $objForm.Topmost = $True

    [void] $objForm.ShowDialog()


    Thank you!



    Friday, May 1, 2015 2:54 AM


  • This gets it.



    $OUNames=Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchBase $OUSub -SearchScope OneLevel | Select-Object -ExpandProperty name 


    • Marked as answer by Duramaxster Friday, May 1, 2015 1:20 PM
    Friday, May 1, 2015 3:18 AM