Solved

Powershell - Pulling all group membership from multiple AD Accounts

Posted on 2014-03-13
4
1,251 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
You're welcome.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…

743 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

15 Experts available now in Live!

Get 1:1 Help Now