combobox displaying @attribute = value

Gareth Davies 21 Reputation points
2020-08-20T13:20:56.573+00:00

I have set up a GUI to allow service desk staff to manage accounts. As part of this I want to have a dropdown box listing all the security goups so they don't have to type the group name and path.
The combobox is populating with the group names, but it is in the format @{name=<group name>}

The powershell code I am using for this is:

{  
$groups = Get-ADGroup -Filter 'GroupCategory -eq "Security"' -Properties name | select name   
$editusersecgroupentrybox.DataSource = $groups  
$editusersecgroupentrybox.DisplayMember = "name"  
$editusersecgroupentrybox.ValueMember = "name"  
  
}  

It gives an error saying it cannot bind to the new display member but it populates the combobox. If I comment out or remove line 6 the error is not seen and the combobox is still populated with the same results
What do I need to change to just display the group name rather than the @{name=.....}

UPDATE:
I tested the add/remove script using the combobox as it is now, it fails because it looks like it is reading the group names as they are displayed. I changed the combobox type to 0 so I could type into the box and edit the text displayed. Even after removing the @{name= and the closing } it errors saying it cannot find an object with identity '@DeezNutz =......} so it is not reading the actual text, it is reading the value it pulled.

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,213 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,455 questions
{count} votes

Accepted answer
  1. Rich Matheisen 45,831 Reputation points
    2020-08-20T18:45:55.133+00:00

    By changing your "select name" to "Select-Object -Expand name" you now have an array of [string] objects instead of an array of [PSCustomObject] objects. If you want an empty string at the beginning of of the array named $groups you'd normally just insert one. However, PowerShell makes that a little harder to do than other languages because the [array] is of fixed length. If your array is short (which it probably is) and isn't being created a gazillion times (which it probably isn't) you can use the following bit of code (which looks unintuitive but it's one of the programming idioms peculiar to PowerShell) that adds a string of one space using array addition:

    $groups = Get-ADGroup . . . | Select-Object . . .
    $groups = ," " + $groups
    

    There are other ways accomplishing the same result, so feel free to experiment. :-)


1 additional answer

Sort by: Most helpful
  1. Gareth Davies 21 Reputation points
    2020-08-20T13:55:48.61+00:00

    I kind of fixed it. Removing everything after the filter, i.e. -properties name | -select name
    resulted in the DN of the groups being displayed which allowed me to add a test user to a group.
    Ideally I would like this to display just the group name but it is workable as it is now.
    If anyone can suggest how I can change what it displays I would be grateful

    0 comments No comments