foreach过滤掉已经分组的成员 [英] foreach filtering out members already apart of group

查看:159
本文介绍了foreach过滤掉已经分组的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $ GetGroupsFromUser = Get-ADPrincipalGroupMembership $ WPFnamelookupbox.Text | 
Where-Object {$ _。Name-like'G1 *'}
$ Groups = Get-ADGroup -Filter {Name -likeG1 *}

foreach $ G $($ G $)$ {
if($ GetGroupsFromUser -contains $ G){
} else {
$ WPFgroups.Items.Add($ G.SamAccountName)
}





我的目标是我只想显示用户不是。

所以我用 -contains 运算符取得了一些进展。为了让 -contains 工作,我需要先创建一个数组,对吗?

解决方案

-contains 数组中的一个元素。



如果您只是在查找与用户没有 的筛选器匹配的组,则可以使用 -notcontains 在where子句中也是如此。

  $ groupFilter =G *
$ user =user_bagel
$ allFilteredGroups = ADGroup -Filter名字式的$ groupFilter| Select-Object -ExpandProperty name
$ userFilteredGroups = Get-ADPrincipalGroupMembership $ user | Where-object {$ _。name-like $ groupFilter} |选择对象-ExpandProperty名称
$ allFilteredGroups | Where-Object {$ userFilteredGroups -notcontains $ _}

您不需要展开组名称正如我所做的那样。无论哪种方式,您都会得到类似的结果。既然你只是想知道这个名字,保持完整的组对象似乎很愚蠢。理论上它也将以更快的速度执行。设置像 $ groupFilter 这样的变量可以更容易地对脚本进行更改。

$GetGroupsFromUser = Get-ADPrincipalGroupMembership $WPFnamelookupbox.Text |
                     Where-Object { $_.Name -like 'G1*' }
$Groups = Get-ADGroup -Filter {Name -like "G1*"}

foreach ($G in $Groups) {
    if ($GetGroupsFromUser -contains $G) {
    } else {
        $WPFgroups.Items.Add($G.SamAccountName)
    }
}

My goal is I want to only show groups that the user is not a member of.

So I made some progress going with the -contains operator. In order for -contains to work, I need to first create an array, correct?

解决方案

-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.

这篇关于foreach过滤掉已经分组的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆