Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Powershell - Pulling all group membership from multiple AD Accounts

Posted on 2014-03-13
4
Medium Priority
?
1,330 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
[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
4 Comments
 
LVL 35

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 41

Accepted Solution

by:
footech earned 2000 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 41

Expert Comment

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Had a business requirement to store the mobile number in an environmental variable. This is just a quick article on how this was done.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
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 …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses

715 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