Avatar of waltforbes
waltforbesFlag for Bahamas asked on

How can I extract unique rows of text from a list in a text file?

Points of My Scenario:


1, I have a text file that lists groups and their members like so:


2. Each group is separated from the other by an empty line, and the group name is always the first item in its list.


3. I need a way to specify a memberX that will output a file listing all the GROUPs under which memberX appears.


QUESTION: What approach can I use to output a list of all groups for any member I specify?

* text filePowershellScripting LanguagesWindows OS* commandline

Avatar of undefined
Last Comment
waltforbes

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
oBdA

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
arnold

Personally, I would revisit the process that generates this list, and use a similar tact to get a user based script that finds a user, and lists the group of which it is a member.

See includes a script that extracts info, that you might find helpful.
https://www.experts-exchange.com/questions/29224763/Powershell-script-to-check-and-list-group-membership-for-users.html
ASKER
waltforbes

@oBdA: If I plan to pass the argument "-Member", should delete line 2 from the PowerShell script?
ASKER
waltforbes

@arnold: the original source & process had to be translated into the current list I now have. Otherwise, your suggestion would be the right approach. Thank you for bringing it forward.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
oBdA

No, that's just the default value for the parameter. If you don't want/need a default value for Member, replace the Param(...) section at the beginning with this:
Param(
	[Parameter(Mandatory=$true)]
	$Member,
	$Path = '.\groups.txt'
)

Open in new window

ASKER
waltforbes

@oBdA: the solution works like a charm! That has saved me at least 3 working days of effort! A big bundle thanks for your help.
arnold

The link actually includes the script that extracts the data into a CSV
and in the case for you, you need only provide the username whose group membership is of interest to you.

There  would not be a need to process the results after the fact.

Could not post the code as it is derivative from the links included in that question.

It lists all users or members of an OU as an input to the data pull.

in your case the feeder is just one user.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
waltforbes

Many thanks for the insight, arnold. Maybe I can provide more details on my scenario:

1. There are 2 separate AD forests (old and new), where old-forest trusts new-forest.

2. I have to migrate old-forest users to new-forest.

3. Accounts for the users have been set up in new-forest.

4. Groups in new-forest have been created to have equivalent permissions of old-forest groups

5. Old-forest group names are inconsistent, but new-forest group names are created with strict naming conventions.

6. I queried old-forest groups for members, then I translated the users and group names into the equivalent in new-forest

7. The resulting [translated] text file is what I am working with in this case.

At this point, if I queried old-forest members for their groups (as you advise), I would get the list I seek, but it would not be in the translated form (new-forest users & groups). I would have to do that translation work (Ctrl+H: i.e. replace) again.
arnold

Interesting though, this all could have been scripted.
you create the translation map, i.e. an array of old groups what they need to be on the new domain.

you can then go through a list of old users, and create the commands to be executed on the new that adds the new user to the respective group on the new domain.

you then run the resulting powershell commands on the new domain, unless you run them after confirming the changes
i.e. looping through the process, and outputing the text as opposed to actually run the command.
ASKER
waltforbes

@arnold: I do agree with you about this approach under 'usual' circumstances.

Here is another multi-piece part of the puzzle:

1. I am an admin in the old-forest ONLY.

2. The new-forest admin 'regime' has created a web application where users can individually request group-memberships

3. The app includes an approval process that  - if user is approved -  then automates their placement into the new-forest groups.

4. My assigned task is to get the applicable list of new-forest groups to each user - so (s)he can individually apply and go through the approval process.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
arnold

My guess is they went the need to know route. providing you with a tidbit each time, effectively causing you to do double the work....
ASKER
waltforbes

I know right! :-)