Hello. I'm relatively new to PowerShell and have only been working with it for a few weeks. I am trying to add the items in an array to a listbox. I am working with Active Directory. I would like to allow the user to select an account after searching by last name in Active Directory. Once they enter the name, I would like to populate a listbox with all the names found in the search results. The code that I have currently returns the names, but it does not put them into the listbox.
Here is the code that I have so far:
Import-Module ActiveDirectory $nameForm = New-Object System.Windows.Forms.Form $nameForm.Text = "Name Entry" $nameForm.Size = New-Object System.Drawing.Size(300,200) $nameForm.StartPosition = "CenterScreen" $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({$nameForm.Close()}) $nameForm.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({$nameForm.Close()}) $nameForm.Controls.Add($CancelButton) $nameLabel = New-Object System.Windows.Forms.Label $nameLabel.Location = New-Object System.Drawing.Size(10,20) $nameLabel.Size = New-Object System.Drawing.Size(280,20) $nameLabel.Text = "Please enter the last name in the space below:" $nameForm.Controls.Add($nameLabel) $nameTextBox = New-Object System.Windows.Forms.TextBox $nameTextBox.Location = New-Object System.Drawing.Size(10,40) $nameTextBox.Size = New-Object System.Drawing.Size(260,20) $nameForm.Controls.Add($nameTextBox) $nameForm.Add_Shown({$nameForm.Activate()}) [void] $nameForm.ShowDialog() $name = $objTextBox.Text $strFilter = "(&(objectCategory=User)(sn=$name))" $objDomain = New-Object System.DirectoryServices.DirectoryEntry $objSearcher = New-Object System.DirectoryServices.DirectorySearcher $objSearcher.SearchRoot = $objDomain $objSearcher.PageSize = 1000 $objSearcher.Filter = $strFilter $colProplist = "name" foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)} $colResults = $objSearcher.FindAll() foreach ($objResult in $colResults) { $names = $objResult.Properties; $names.name } [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") $nameListBox = New-Object System.Windows.Forms.ListBox $nameListBox.Location = New-Object System.Drawing.Size(10,40) $nameListBox.Size = New-Object System.Drawing.Size(260,20) $nameListBox.Height = 80 [void] $nameListBox.Items.Add("$names") $lbForm = New-Object System.Windows.Forms.Form $lbForm.Text = "Name Entry" $lbForm.Size = New-Object System.Drawing.Size(300,200) $lbForm.StartPosition = "CenterScreen" $lbForm.Controls.Add($nameListBox) $lbForm.Topmost = $True $lbForm.Add_Shown({$lbForm.Activate()}) [void] $lbForm.ShowDialog() [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
Any help would be greatly appreciated. Thank you.