Solved

Powershell - Pulling all group membership from multiple AD Accounts

Posted on 2014-03-13
4
1,259 Views
Last Modified: 2014-03-14
I have the script below that pulls all the group membership for the users listed in the text file, but the output is not that great ...

Add-PSSnapin *Quest* -ErrorAction SilentlyContinue
$input= "e:\Projects\Users\users.txt"
$Output = "e:\Projects\Users\Users_Groups.csv"
Get-Content $input | % {
$user = Get-QADUser -sAMAccountName $_
New-Object Psobject -Property @{"UserName"=$user.Name;"Groups"="";"UsrSAmAccountName"=$user.SAmAccountName;"USRDescription"=$user.Description;"GrpDescription"=""}

ForEach ($Group in $user.memberof) {
            $Grp= Get-QADGroup $group
            New-Object Psobject -Property @{"UserName"="";"Groups"=$Grp.Name;"UsrSAmAccountName"="";"USRDescription"="";"GrpDescription"=$Grp.Description}
    }
} | Export-CSV -Path $Output -NoTypeInformation


Does anyone have one that pulls the data to similar to what is listed below ?? so basically I need the SamAccountName to be one each row next to each group it is a member of.

Name         SamAccountName       Groups               Group Description
Test User         ABC123                       AccessGroup1      
         ABC123                       AccessGroup2      
         ABC123                       AccessGroup3      
         ABC123                       AccessGroup4      
TestUser2         ABC456                       AccessGroup5      
         ABC456                       AccessGroup6      
         ABC456                       AccessGroup7
Capture1.PNG
0
Comment
Question by:MilesLogan
  • 2
4 Comments
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39928509
Can you post a sample result from your current code? I don't have a station here to test, but I can probably modify the existing code to fit your needs, if I see the current output.

Thanks,
Dan
0
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
ID: 39928718
I prefer to help people understand the code they're working with rather than just providing a script.  If all you want is a script, let me know and I'll just bow out.

In the .CSV that you are generating with your code above, each line is an object.  Each object is created by the New-Object cmdlet, with the properties as specified.  So if you want each object to include the user's samaccountname, just modify the code where you are defining what that property contains.  This would mean changing the second New-Object command so that the "UsrSamAccountName" property does not equal an empty string (""), but instead is more like the previous New-Object command where that property is filled.

If you have any issue with the ordering of columns when piping to Export-CSV, you should first pipe to a Select-Object command and manually define the order of the properties (see below)
| Select UserName,UsrSAmAccountName,USRDescription,Groups,GrpDescription | Export-CSV file.csv -notype

Open in new window


I'll generally advocate outputting to a .CSV when you want all columns to contain the same information (i.e. it is actually useful as a .CSV).  When each object contains different properties, and the .CSV is used more just as an intermediate so that Excel displays things prettily, I lean towards just outputting a text file with all the formatting that you want.  This is the difference between outputting a .CSV that can be reused vs. generating a report that is the final product.
0
 
LVL 2

Author Closing Comment

by:MilesLogan
ID: 39929800
Thank you much fooTech ..

I changed the line to "UsrSAmAccountName"=$user.SAmAccountName; and it changed the output to what I needed ..  thanks !
0
 
LVL 39

Expert Comment

by:footech
ID: 39930174
You're welcome.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Powershell script 13 74
Changing bootstarp.ini in MDT throws errors 3 20
who removed AD Domain ID 9 28
will this script work on w2k3 server? 2 13
In this article, we will see the basic design consideration while designing a Multi-tenant web application in a simple manner. Though, many frameworks are available in the market to develop a multi - tenant application, but do they provide data, cod…
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
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…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

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

24 Experts available now in Live!

Get 1:1 Help Now