Reputation: 13
Bear with me on my first question on any online forum ever. =]
My intent is to use PowerShell to create a csv with the following information:
Currently, My script is as follows.
Get-ADGroup -filter {name -like "O365*"} -properties GroupCategory |
Select-Object -ExpandProperty name |
get-adgroupmember |
where {$_.objectclass -eq 'user'} |
Get-ADUSer -properties Displayname,Description,Mail |
Select Name,Displayname,Mail
This result provides me with:
I feel as though I have missed the answer time and time again after researching this for a while. Most people seem content to do this similar task without filtering the ADGroup cmdlet, but that would take too long for me.
As a newbie, I am excited to hear from anyone who'll take the time to answer. Thank you.
Upvotes: 1
Views: 1885
Reputation: 601
So you want the users that are the member of a group with a name matching "O365* And they need to be displayed as
Group.Name | User.DisplayName | User.Mail
Simply looking at the question one could think the following code would do the trick and it works but there is a catch ......
cls
$ErrorActionPreference = "stop"
#$VerbosePreference = "Continue"
#$DebugPreference = "Continue"
Write-Verbose "Retrieving the ADGroups"
$Groups = Get-ADGroup -filter {name -like "O365*"}
Write-Debug ($Groups | Select Name | Format-Table | Out-String)
$UserList = @()
Foreach ($Group in $Groups) {
Write-Verbose "Processing Group $Group"
$GroupMembers = $Group | Get-ADGroupMember
Write-Debug "GroupMembers: $($GroupMembers | Select Name | Format-Table | Out-String)"
Foreach ($GroupMember in $GroupMembers) {
Write-Verbose "Processing GroupMember $GroupMember"
Write-Debug "GroupMember $($GroupMember | Out-String)"
if (!($GroupMember.objectClass -eq "user")) {
Write-Warning "GroupMember $($GroupMember) is not of type 'User', Skipping"
Continue
}
else {
Try {
$UserList += ($GroupMember | Get-ADUSer -properties Mail | Select @{N="Group"; E={$Group.Name}}, Name, Mail)
Write-Verbose "user $GroupMember has been added to the list."
}
Catch {
Write-Warning "An error ocurd while adding user $GroupMember to the list."
Continue
}
}
}
}
$UserList
If the o365 groups contain other groups then the members of these subgroups will not be included.
Output Example
WARNING: GroupMember CN=subgroup,OU=Custom,DC=DOMAIN,DC=TLD is not of type 'User', Skipping
Group Name Mail
----- ---- ----
o365-SubscriptionA User U. UserLand [email protected]
o365-SubscriptionA User2 U2. UserLand [email protected]
o365-SubscriptionB User U. UserLand [email protected]
Upvotes: 1