I ran into two issues:
- Get-Contact does not return group memberships for contacts. The good news is you can work around this by using Get-ADObject -Filter {ObjectClass -eq "contact"} -Properties MemberOf.
- The MemberOf attribute is returned as an array which does not export properly to a CSV file. For this you need to create a new calculated property that is a string.
Get-ADObject -Filter {ObjectClass -eq "contact"} -Properties MemberOf | Select-Object Name,@{Name='Groups';Expression={$_.MemberOf -join ";"}} | Export-CSV C:\contacts.csv
Let's break this down a bit:
- The -Properties parameter is used to specify an AD attribute that you want to retrieve for an object. By default, the Get-ADObject retrieves only a default set of attributes. You need to use -Properties to get more than that. You can use -Properties * to get all attributes.
- The Select-Object cmdlet limits the properties collected for each contact. Prunes them down to the Name property and a new calculated property Groups.
- The creation of calculated property Groups is within the @{}. This Name='Groups' defines the name. The Expression={} defines the value of the property. In this instance, it takes each instance of the MemberOf array and joins it together into a single value separated by a semicolon. Each contact will have a single Groups attribute with a single value which is a long concatenated list of the groups that contact is a member of.
- Export-CSV dumps the list of contacts and their group memberships to a CSV file that is easy to sort in Excel.
Get-Contact -Filter {MemberOfGroup -eq $null}
No comments:
Post a Comment