Solved

Need a script  that gets group name and member

Posted on 2014-10-22
6
539 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
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
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 …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

821 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