I’m having a PowerShell syntax issue: I can’t seem to match members of one array to another array, but I can confirm the members exist in both arrays. Can someone point out my logic error?
Background:
I want to search Entra for all members of a specific group, based on a search string of the member’s properties (e.g. DisplayName and Surname). Given limitations to the Get-Mg* -filter calls, I’ve come up with this approach:
$groupName = 'staff' # all results should be in staff group
$userMatchString = 'Be' # e.g. [Be]n Smith, Jim [Be]am...
$domainMatch = 'example.com' # custom domain - no Entra guests
## choose a central M365 group filter
$group = get-mggroup -Filter "DisplayName eq '$($groupName)'"
$groupMemberIds=Get-MgGroupMember -GroupId $group.id -ConsistencyLevel eventual -All | select Id
## find Entra users matching a search string
## Due to graph filter limitations we do separate calls per attribute
$userMatches=@(Get-MgUser -ConsistencyLevel eventual -Count userCount -Filter "startsWith(DisplayName, '$($userMatchString)')" | where { $_.Mail -like "*@$($domainMatch)"})
$userMatches+=@(Get-MgUser -ConsistencyLevel eventual -Count userCount -Filter "startsWith(Surname, '$($userMatchString)')" | where { $_.Mail -like "*@$($domainMatch)" })
At this point $groupMemberIds
contains every qualifiable person to search for and $userMatches
contains every Entra search result (that may or may not be in the group).
This confirms (for my resultset) that the first record is already a match: $groupMemberIds | where { $_.Id -eq $userMatches[0].Id }
Can someone help me with syntax for (any userMatches ) in (groupMemberIds)?
This doesn’t work – it returns ‘false’ for all of them:
$userMatches | ForEach-Object { $($_.Id -in $groupMemberIds) }
Any ideas? Thanks!