PowerShell not matching strings

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.

I cannot reproduce …

It might be something on your machine. :wink: :man_shrugging:

As a test I would output what isn’t matching, but append a character to the ends. My guess would be potentially a non-printable char or space on either end.

1 Like

Have you tried to output the group name you get from your AD? The culprit might be there - not in the string you place manually in your $CommonGroups variable.

It could be an ambiguous character …

'GPAP-M365-E3―Base'
'GPAP-M365-E3-Base'

These two strings are not equal!! :wink:

image

I did, or swear I did, copy the name from the AD group object and pasted it into the array of Common Groups and it still was not working. I was just about to write a little function to display the ASCII character codes for each character in the strings, but before I did that, I made another change. When I populate the $CommonGroups, I use a fixed list of group names and then append the results from a SQL query and the name returned by several (Get-ADGroup -Filter { Name -like "blah*" }).Name statements. What I did this morning was to add one more Get-ADGroup where the pattern matched GPAP-M365-* and now it is working. At some point in time, before the group was created, it must have been in Word or Outlook and Microsoft “helped” with the name. I am going to have to run through these group names now and rename them in AD. Oh joy.

Sometimes we can’t see the forest for the trees … that happens to all of us, I think. :man_shrugging:

I’m glad you’ve found the issue. :+1: