I run into this every once in a while, remove some hair from my head and find another way to do it and move on. This time I really just want to know what is going on.
I have a function that populates a variable with an array of strings. In this case, the name of AD groups. I have another function that pulls the group membership of a user. This function returns AD group objects. I then run the group objects through a ForEach and a Where-Object to remove the groups whose name appears on the first list. Like this:
$CommonGroups = @("Group1","Group2","Hard-workers","Dirt-Bags","Suck-ups","Good-guys")
Function Get-FilteredGroupMembership {
param(
$UserId
)
$Groups = Get-CAMUserGroupMembership -UserID $UserId
# Now go through these to see if I need to filter any of these.
$Groups | ForEach-Object { $_ | Where-Object { $_.Name -notin $CommonGroups } }
}
The Get-CAMUserGroupMembership
function returns an array of AD group objects from that the user is a member of.
The function works great, for the most part. There seems to be one group, so far, that does not filter out. The group’s name is GPAP-M365-E3-BASE
and it is added to the $CommonGroups list. However, when a user who is in that group passes through the Get-FilteredGroupMembership
function, the other groups are removed, but not GPAP-M365-E3-BASE
I am just confused.
I have tried $CommonGroups | Where-Object { $_ -eq "GPAP-M365-E3-Base" }
and that returns GPAP-M365-E3-BASE
, so I know it is in there.
When I execute "GPAP-M365-E3-BASE" -in $CommonGroups
I get False, but when I pick a different group, that I know is in the list, I get True.
I know that one might assume some whitespace, so I tried Trim, no dice. I copied the name from the group and pasted it into the list (just in case there was a unicode character in there or something) and I still get false.
Does anyone have a suggestion on what might be wrong? There are 1044 elements in the $CommonGroups array and all of users I have run through this function appear to get their groups filtered as expected, except for this group.