Solved

Powershell - Pulling all group membership from multiple AD Accounts

Posted on 2014-03-13
4
1,299 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 40

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 40

Expert Comment

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

Featured Post

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
This tutorial will give a short introduction and overview of Backup Exec 2012 and how to navigate and perform basic functions. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as conne…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

738 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