Solved

Need a script  that gets group name and member

Posted on 2014-10-22
6
534 Views
Last Modified: 2014-10-22
Hello,
I need a script  that gets security group name and member information. I want to use excel file with security groups listed that I need to get members for. The export that I would like to get should include both group name and its members. I already tried the following, but it only gets the members without group name.

$Users = Import-CSV -Path C:\Temp\GroupsTest.csv

$Users | %{Get-ADGroupMember –identity $_.Group} | select name | export-csv c:\temp\SecurityGroupMembers10-22.csv

Below is the excel file I am using:
It has just one column "group" that lists the name of the security groups that I need to get members for.
Group:
Group1
Group2

I am new to powershell, so please explain in details what each line means.

I also tried the following already, but it doesn’t work:
Get-ADGroup -Filter {GroupCategory -eq "Security" -and Name -like "*"}| %{ $Group = $_.Group Get-ADGroupMember $Group | Select @{N="GroupName";E={$Group}},@{N="Member";E={$_.Group}}" }| Export-Csv C:\Users\Desktop\AD\Report.csv -nti Get-ADUser -Filter * -properties * | select fullname*, username*, disp*,distinguishedName*,GroupName,GroupCategory,Member
0
Comment
Question by:claudiamcse
  • 2
  • 2
  • 2
6 Comments
 

Author Comment

by:claudiamcse
ID: 40397253
I get the error below: Unexpected token 'Get-ADGroupMember' in expression or statement.

 $Users | %{Get-ADGroup -identity $_.Group}| %{ $Group = $_.Group Get-ADGroupMember $Group
Unexpected token 'Get-ADGroupMember' in expression or statement.
At line:1 char:83
+ $Users | %{Get-ADGroup -identity $_.Group}| %{ $Group = $_.Group Get-ADGroupMember <<<<  $Group
    + CategoryInfo          : ParserError: (Get-ADGroupMember:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken

$Users | %{Get-ADGroup -identity $_.Group}| %{ $Group = $_.Group Get-ADGroupMember $Group
0
 
LVL 29

Expert Comment

by:becraig
ID: 40397328
You need to separate the different commands:

$Group = $_.Group;  Get-ADGroupMember $Group 

Open in new window


a semi-colon does this or add a new line.
0
 
LVL 16

Accepted Solution

by:
Joshua Grantom earned 250 total points
ID: 40397349
Here is a shorter version that will perform the same thing

$Users = Import-CSV -Path C:\groups.csv
$Users | % {
$group = $_.Group 
Get-ADGroupMember –identity $group} | select @{Name="Group Name";Expression={$group}},@{Name="Member";Expression={$_.Name}} | export-csv c:\SecurityGroup.csv -nti

Open in new window


EDIT: Just noticed you wanted more member info, what information are you wanting?
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 29

Assisted Solution

by:becraig
becraig earned 250 total points
ID: 40397395
Requested explanation of what the lines do:


#this line call into AD and queries for all groups that are of the type "Security" with ANY name "*"  then pipes it into a fireach loop ( | %)
Get-ADGroup -Filter { GroupCategory -eq "Security" -and Name -like "*" } | % {

#This next line assigns a variable of group to value $_.group retrieved from your query
     $Group = $_.Group;
#You then run a  query with get-adgroupmember to get all the members for each security group returned from the query above using the variable $group (you just defined as the name).  The you proceed to assign labels and values to a hash table before creating the output to your csv (Select @{ N = "GroupName"; E = { $Group } })    [This says $group will have a label / header of "Groupname"]

   Get-ADGroupMember  $Group | Select @{ N = "GroupName"; E = { $Group } }, @{ N = "Member"; E = { $_.Group } }

Finally you export your results to csv.
} | Export-Csv C:\Users\Desktop\AD\Report.csv -nti
0
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40397431
Here is one with more info.

$Users = Import-CSV -Path C:\groups.csv
$Users | % {
$group = $_.Group 
Get-ADGroupMember –identity $group} | select @{Name="Group Name";Expression={$group}},@{Name="Member Name";Expression={$_.Name}},@{Name="SamAccountName";Expression={$_.SamAccountName}},@{Name="Distinguished Name";Expression={$_.DistinguishedName}} | export-csv c:\SecurityGroup.csv -nti

Open in new window

0
 

Author Closing Comment

by:claudiamcse
ID: 40397858
Thank you so much! Excellent!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
This article outlines the process to identify and resolve account lockout in an Active Directory environment.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…

778 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