?
Solved

Get-Adgroupmember

Posted on 2016-09-14
11
Medium Priority
?
91 Views
Last Modified: 2016-09-14
I will like to import a bulk  of AD groups, and get the all the members that are locating inside of it.  Can some provide me the correct syntax for this.  I will like to use the Foreach Cmdlt to do this work.
0
Comment
Question by:JCJohnson76
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 16

Expert Comment

by:FOX
ID: 41798281
List the names of your groups and at the top put a header named Groups and name it groups.csv

Import-Csv 'c:\filelocation\groups.csv' | Foreach{Get-ADGroupMember -identity $_.Groups -recursive | select name | Export-csv -path 'C:\filelocation\Groupmembers.csv' -NoTypeInformation
0
 
LVL 40

Expert Comment

by:Subsun
ID: 41798282
Try..
Import-Module Activedirectory
Get-ADGroup -F * | %{$name = $_.Name; $_ | Get-ADGroupMember -Recursive | Select @{N="Group";E={$name}},@{N="Member";E={$_.Samaccountname}},ObjectClass} | Export-csv C:\Groupreport.csv -nti

Open in new window

Result will have all groups and it's members in CSV format. If you want to filter the members or groups from result that also can be done..
0
 

Author Comment

by:JCJohnson76
ID: 41798356
Import-Csv 'c:\filelocation\groups.csv' | Foreach{Get-ADGroupMember -identity $_.Groups -recursive | select name

This is only displaying the user list that is located in the groups.  I will like for it to display user list and the Group Name the user is located inside
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 40

Expert Comment

by:Subsun
ID: 41798376
@JCJohnson76, Did you check the code which I posted?
0
 

Author Comment

by:JCJohnson76
ID: 41798386
Subsun,

That code provide a broad scope of all groups.  The code I'm looking for is where I specify a range of  groups listed in a csv file.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 41798402
You didn't mention, you need to read from a input file.. :-)
Try this modified code..
Import-Module Activedirectory
Import-csv C:\input.csv | %{$name = $_.GroupName; Get-ADGroupMember $_.GroupName -Recursive | Select @{N="Group";E={$name}},@{N="Member";E={$_.Samaccountname}},ObjectClass} | Export-csv C:\Groupreport.csv -nti

Open in new window


Sample input CSV
GroupName
GroupA
GroupB

Open in new window

0
 

Author Comment

by:JCJohnson76
ID: 41798429
And yes I populated the csv file with the correct header

Received a bunch of these errors

Get-ADGroupMember : Cannot validate argument on parameter 'Identity'. The argument is null or empty. Provide an argument that is not null
or empty, and then try the command again.
At line:2 char:78
+ Import-csv C:\csv\fin_groups.csv | %{$name = $_.GroupName; Get-ADGroupMember $_. ...
+                                                                              ~~~
    + CategoryInfo          : InvalidData: (:) [Get-ADGroupMember], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADGroupMember
0
 

Author Comment

by:JCJohnson76
ID: 41798435
Subsun
this is the code that i used which you provided

Import-Module Activedirectory
Import-csv C:\csv\fin_groups.csv | %{$name = $_.GroupName; Get-ADGroupMember $_.GroupName -Recursive | Select @{N="Group";E={$name}},@{N="Member";E={$_.Samaccountname}},ObjectClass} | Export-csv C:\csv\Groupreportt.csv -nti
0
 
LVL 40

Accepted Solution

by:
Subsun earned 2000 total points
ID: 41798496
Get-ADGroupMember : Cannot validate argument on parameter 'Identity'. The argument is null or empty. Provide an argument that is not null
Means the input value is null, probably the input file has some trailing spaces..


Import-csv C:\input.csv | ?{$_.GroupName}|%{$name = $_.GroupName; Get-ADGroupMember $_.GroupName -Recursive | Select @{N="Group";E={$name}},@{N="Member";E={$_.Samaccountname}},ObjectClass} | Export-csv C:\Groupreport.csv -nti

Open in new window

0
 

Author Comment

by:JCJohnson76
ID: 41798546
Subsun,

Thanks, Horray,

Works like a charm
0
 

Author Closing Comment

by:JCJohnson76
ID: 41798547
Outstanding!!!!!
0

Featured Post

Ransomware Attacks Keeping You Up at Night?

Will your organization be ransomware's next victim?  The good news is that these attacks are predicable and therefore preventable. Learn more about how you can  stop a ransomware attacks before encryption takes place with our Ransomware Prevention Kit!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question