【问题标题】:Find users who don't belong to multiple groups查找不属于多个组的用户
【发布时间】:2021-02-12 00:17:50
【问题描述】:

我的公司使用 Microsoft Intune。我们在控制条件访问的本地 AD 中有 4 个组。我们只称它们为 AllowGroup1AllowGroup2BlockGroup1BlockGroup2。我想知道找到的是不在所有组中的所有用户。我想要找到的结果是任何不在上述组中的用户对象。这样我就可以证明我们的整个系统是合规的。下面是我从这篇文章List AD Users who do not belong to one of several groups借来的 Powershell 代码

我正在我的家庭域控制器上运行这些测试。我遇到的问题是脚本没有在整个域中查找用户。具体来说,我的个人 DC 中有一个名为 Home 的 OU(我创建了 OU),并且在一个名为 Users 的子 OU 中有 2 个用户对象,该脚本是不拉。我正在使用 Enterprise Admins 组中的用户运行此脚本,因此我知道它具有足够的权限。它应该通过 PowerShell 在 AD 中搜索不在多个组中的用户,并将这些用户放在一个名为 NotInGroup 的组中

更详细地说,一些用户将在 AllowGroup1BlockGroup2 中。一些用户将在 BlockGroup1BlockGroup2 中。我想查找所有不在上面列出的任何组中的用户。

Import-Module ActiveDirectory
$groupname = "NotInGroup"
$members = Get-ADGroupMember -Identity $groupname

    foreach($member in $members)
    {
     Remove-ADGroupMember -Identity $groupname -Member $member.samaccountname
    }
    $users = Get-ADUser -Filter 
    {
        ((memberof -notlike "CN=AllowGroup1,OU=Intune,OU=Groups,DC=domain,DC=local") 
    -AND (memberof -notlike "CN=AllowGroup2,OU=Intune,OU=Groups,DC=domain,DC=local")
    -AND (memberof -notlike "CN=BlockGroup1,OU=Intune,OU=Groups,DC=domain,DC=local") 
    -AND (memberof -notlike "CN=BlockGroup2,OU=Intune,OU=Groups,DC=domain,DC=local"))
     } 
    -SearchBase "dc=domain,dc=local" -SearchScope Subtree
    
    foreach($user in $users)
    {
    Add-ADGroupMember -Identity $groupname -Members $user.samaccountname -ErrorAction SilentlyContinue
    }

【问题讨论】:

    标签: powershell active-directory user-management


    【解决方案1】:

    我认为这样的复杂过滤器不会起作用,我会选择使用正则表达式。

    可能是这样的

    # get users not in groups 'AllowGroup1', 'AllowGroup2', 'BlockGroup1', 'BlockGroup2'
    $regex = 'CN=(AllowGroup[12]|BlockGroup[12])'
    $users = Get-ADUser -Filter * -Properties MemberOf | Where-Object { ($_.MemberOf -join ';') -notmatch $regex }
    

    或者您可以尝试使用LDAPFilter 参数:

    $filter = '(!(|(memberof=CN=AllowGroup1,OU=Intune,OU=Groups,DC=domain,DC=local)
                   (memberof=CN=AllowGroup2,OU=Intune,OU=Groups,DC=domain,DC=local)
                   (memberof=CN=BlockGroup1,OU=Intune,OU=Groups,DC=domain,DC=local)
                   (memberof=CN=BlockGroup2,OU=Intune,OU=Groups,DC=domain,DC=local)))'
    $users = Get-ADUser -LDAPFilter $filter
    

    FilterLDAPFilter 两个参数都需要一个字符串,而不是脚本块

    【讨论】:

    • 明白了。我遇到了使用 -notlike 而不是 (!( 的错误语法。感谢帮助!
    • @bobthespoon 很高兴听到这对您有所帮助。作为平台的新手,您可能不知道这一点,但习惯上通过单击答案左侧的复选标记图标来接受解决了您的问题的答案。这有助于其他有类似问题的人更轻松地找到它。
    猜你喜欢
    • 1970-01-01
    • 2019-04-13
    • 2016-07-18
    • 2021-07-30
    • 2021-01-18
    • 1970-01-01
    • 2013-02-27
    • 2013-09-17
    • 2011-08-21
    相关资源
    最近更新 更多