Solved

Need a script  that gets group name and member

Posted on 2014-10-22
6
551 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
[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
  • 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

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

This article describes my battle tested process for setting up delegation. I use this process anywhere that I need to setup delegation. In the article I will show how it applies to Active Directory
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

738 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