Hi All,
I have been in the middle of creating a group membership report for the management. I managed to get the report using powershell but spent hours formatting it the way I wanted. I am very new to powershell so here goes nothing.
I am looking to get report based on a few conditions and the csv has to be in a certain format. These users have to be active and from a certain OU which has space like OU=My Test OU,DC=Subdom,DC=dom,DC=tld. All group names are different but start with lets say “GPA_”
First Condition : There are 10 odd groups. If the user belongs to any one of these groups. Then the CSV has to be in below format
username AccessA AccessB AccessC
userA Disabled Disabled Disabled
Second Condition: There are 3 groups. If the user belongs to one or more than one group than accordingly it will be
username AccessA AccessB AccessC
userB Enabled Disabled Disabled
or
username AccessA AccessB AccessC
userB Enabled Enabled Disabled
depending one which group and how many groups it is a member of.
Third Condition: If the any user is not a member of any group that start with GPA_* then the CSV will be formatted as
username AccessA AccessB AccessC
userC Enabled Enabled Enabled
If everything can be in one CSV then it would be great.
I used the below powershell scripts to get the data i needed,
Powershell one for condition one and two
$CustomResult=@() $groups = Get-Content "C:\\groups.txt" $Groups | ForEach-Object { $group = $_ Get-ADGroupMember $group | ForEach-Object { $CustomResult += [PSCustomObject] @{ GroupName = $group Username = $_.samAccountName Member = $_.Name } } } $CustomResult | Export-CSV "C:\\ad-group-members.csv" -NoTypeInformation -Encoding UTF8
Powershell 2 for third condition
Get-ADUser -SearchBase "OU=My Test OU,DC=Subdom,DC=dom,DC=tld" -Filter * -properties memberof | Where-Object {!($_.memberof -like "*GPA_*")} | Select-Object SamAccountName | Export-CSV "C:\\fullaccess-members.csv"
Any help will be great.Thanks in advance.