question

GarethDavies-0557 avatar image
0 Votes"
GarethDavies-0557 asked GarethDavies-0557 commented

combobox displaying @attribute = value

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 '@name=......} so it is not reading the actual text, it is reading the value it pulled.

windows-server-powershellwindows-active-directory
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I haven't tested it, but try changing your "select name" to "Select-Object -Expand name".

0 Votes 0 ·

that worked, thank you.

Now I have another question. How can I make the top line of the dropdown blank? I need to make sure the person running this actually makes a selection and doe snot just add the user to the first group in the list

0 Votes 0 ·
RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered GarethDavies-0557 commented

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

that works perfectly, it takes a little longer to open the GUI but that is not a big deal.
Thank you

0 Votes 0 ·
GarethDavies-0557 avatar image
0 Votes"
GarethDavies-0557 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.