0
votes

Here is the situation, the auditors are in and asking for a report for all the users in certain groups with some account information plus which group they are in. So what I did was to put all the requested groups in a txt file and use Get-Content and pipe it to a ForEach to do a Get-ADGropuMember to find all the users then a Get-ADUser in order to get all the properties they asked for. The problem I'm having is that by the time I get that far into piping I no longer have the original group that triggered why the user account was selected. Basically the script I have come up with so far:

Get-Content '.\Scripts - Input\ADGroupIn.Accounts.txt' | ForEach-Object { Get-ADGroupMember $_ -Recursive -Server Accounts | Get-ADUser -Properties SAMAccountName, Enabled, Name, Description, LastLogonDate, whenCreated | Select-Object SAMAccountName, Enabled, Name, Description, LastLogonDate, whenCreated | sort-object SAMAccountName} | Export-csv ".\Scripts - Output\SelectedGroups_Accounts.csv" -NoTypeInformation

"Domain Admins" is a group name being read into the Get-Content. The Get-ADGroupMember pulls in UserA, UserB, and UserC. Then the Get-ADuser gets all the properties for each user but at this time I want to be able to append "Domain Admins" to the information for each user. If I use the MemberOf property I will get all the groups the users are a member of when I need to show it was "Domain Admins" that put them on the list.

I haven't been working with Powershell for very long and would appreciate any help you can give me.

1

1 Answers

0
votes

try this

Get-Content '.\Scripts - Input\ADGroupIn.Accounts.txt' | ForEach-Object { 
    $group = $_
    Get-ADGroupMember $group -Recursive -Server Accounts |
    Get-ADUser  -Properties SAMAccountName, Enabled, Name, Description, LastLogonDate, whenCreated | 
    Select-Object SAMAccountName, Enabled, Name, Description, LastLogonDate, whenCreated, @{n='Group';e={$group}} | 
    sort-object SAMAccountName
} | Export-csv ".\Scripts - Output\SelectedGroups_Accounts.csv" -NoTypeInformation