Solved

Need a script  that gets group name and member

Posted on 2014-10-22
6
526 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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
In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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 to another domain controller. Log onto the new domain controller with a user account t…

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now