foreach filtering out members already apart of group

前端 未结 2 1156
你的背包
你的背包 2021-01-25 16:37
$GetGroupsFromUser = Get-ADPrincipalGroupMembership $WPFnamelookupbox.Text |
                     Where-Object { $_.Name -like \'G1*\' }
$Groups = Get-ADGroup -Filter {N         


        
相关标签:
2条回答
  • 2021-01-25 17:31

    -contains functions best when you are trying to find a match of an element in an array.

    If you are just looking for the groups that matches a filter that a user does not already have we can use -notcontains inside a where clause as well for this.

    $groupFilter = "G*"
    $user = "user_bagel"
    $allFilteredGroups = Get-ADGroup -Filter "name -like '$groupFilter'" | Select-Object -ExpandProperty name
    $userFilteredGroups = Get-ADPrincipalGroupMembership $user | Where-object{$_.name -like $groupFilter} | Select-Object -ExpandProperty name
    $allFilteredGroups | Where-Object{$userFilteredGroups -notcontains $_}
    

    You don't need to expand the groups names as I have done. You will get similar results either way. Since you only wanted to know the names it seemed silly to keep the complete group object. In theory it will also perform faster this way. Setting up variables like $groupFilter makes it easier to make changes to your script down the line.

    0 讨论(0)
  • 2021-01-25 17:39

    You could use Compare-Object:

    $GetGroupsFromUser = Get-ADPrincipalGroupMembership $WPFnamelookupbox.Text | Where-Object {$_.name -like 'G1*' }
    $Groups = Get-ADGroup -Filter "name -like 'G1*'"
    Compare-Object $Groups $GetGroupsFromUser | Where-Object {$_.SideIndicator -eq "<="}
    
    0 讨论(0)
提交回复
热议问题