Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Powershell - Pulling all group membership from multiple AD Accounts

Posted on 2014-03-13
4
Medium Priority
?
1,347 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 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Microsoft Office 365 is a subscriptions based service which includes services like Exchange Online and Skype for business Online. These services integrate with Microsoft's online version of Active Directory called Azure Active Directory.
Wouldn't it be nice if objects in Active Directory automatically moved into the correct Organizational Units? This is what AutoAD aims to do and as a plus, it automatically creates Sites, Subnets, and Organizational Units.
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.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

971 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