Help getting a table with a user's group (direct and indirect) membership with Powershell

Hello Experts,
Once again, I’m coming to you with what I’m sure is going to be an easy question that I’m pulling my hair out over.  I want to generate a list of all the groups that all of the members of our technical sales department belongs to and create a csv file with that information.  The basic output would look like the following:
TSD Member                        Group
Nickd                              Domain Users
Nickd                              TSD
Nickd                              Sales-General
JoeF                              Domain Users
JoeF                              TSD
JoeF                              Sales-General
JoeF                              Sales-North
KrisG                              Domain Users
KrisG                              TSD
KrisG                              TSD-Marketing
KrisG                              TSD-Sales      

Here is the code I have to get users of the technical sales division (I know it si messy, but if filters the way I need it to in our Active Directory):
$TSDMembers = Get-ADObject -Properties * -Filter {objectclass -eq "user" -and objectCategory -eq "Person"} -SearchBase "OU=Technical Sales division,OU=phoenix,DC=mycompany,DC=local" | where-object {$_.st -eq "AL"} | select -ExpandProperty samaccountname | sort samaccountname

Foreach ($TSDMember in $TSDMembers){
Get-QADGroup -ContainsMember $TSDuser.samaccountname -ContainsIndirectMember $TSDuser.samaccountname | select `
    @{n="Name";e="$TSDuser.samaccountname"},
    @{n="Groups";e="$_.name"}
}

When I run this, my table has the column headers, but no data.  What am I doing wrong here?

Thanks in advance for your help.
LVL 1
ndalmolin_13Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SubsunCommented:
$TSDuser.samaccountname won’t return any value First it is not defined in the script, you have used $TSDMember as a variable in Foreach but inside loop you were using $TSDuser which was not defined anywhere in script.
 Secondly, you have used Select –ExpandProperty samaccountname to create the list of samaccountname’s, so the property name samaccountname will not be available as the array $TSDMembers you create is just a list of strings (list of samaccountname)

The custom property syntax was incorrect..

Lastly, not need to use both -ContainsMember & -ContainsIndirectMember. -ContainsIndirectMember parameter will give you the details which you are looking for..

I also replaced Get-ADObject with Get-QADObject
Try..
$TSDMembers = Get-QADObject -Type User -SearchRoot "OU=Technical Sales division,OU=phoenix,DC=mycompany,DC=local" | where-object {$_.st -eq "AL"} | select -ExpandProperty samaccountname | sort samaccountname

Foreach ($TSDMember in $TSDMembers){
Get-QADGroup -ContainsIndirectMember $TSDMember | select `
    @{n="User";e={$TSDMember}},
    @{n="Groups";e={$_.name}}
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ndalmolin_13Author Commented:
Awesome.  Thanks for the explanation.  It really helped.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.