How to list active directory groups in linux for a given user, one per line, knowing that some group name contain a space character

active-directorycommand-line-interfacegroupsunixworkgroup

When using "groups" or "id -Gn", I end up with the typical space-delimited list of all groups for the current user.
These commands run on the assumption that group names cannot contain a space character, and indeed, as long as we stay within Unix, it's going to be the case.

However, my company is now part of a bigger one, that has Microsoft domains setups, and unfortunately, their Active Directory domain group names contain a space character, like "FOOBAR\Domain Users".

One of our scripts typically uses "groups" output and makes a list out of it, based on that space-character delimiter, which means that it now fails miserably:

$ groups
FOOBAR\Domain Users FOOBAR\Other Domain everyone admin 

… which obviously ends up producing such list:

FOOBAR\Domain
Users
FOOBAR\Other
Domain
everyone
admin

As you can imagine, the first 4 groups don't exist and the rest of the script fails to achieve anything of value.

Does anyone know where to obtain such group names in a better way?

PS: I know of /etc/group but such AD groups aren't mentioned there. Would there be another file like this, but for AD groups, that I could parse?

Best Answer

old post that I randomly ran across, but my current method is

id | egrep -o 'groups=.*' | sed 's/,/\n/g' | cut -d'(' -f2 | sed 's/)//'