【发布时间】:2021-02-12 00:17:50
【问题描述】:
我的公司使用 Microsoft Intune。我们在控制条件访问的本地 AD 中有 4 个组。我们只称它们为 AllowGroup1、AllowGroup2、BlockGroup1 和 BlockGroup2。我想知道找到的是不在所有组中的所有用户。我想要找到的结果是任何不在上述组中的用户对象。这样我就可以证明我们的整个系统是合规的。下面是我从这篇文章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 的组中
更详细地说,一些用户将在 AllowGroup1 和 BlockGroup2 中。一些用户将在 BlockGroup1 和 BlockGroup2 中。我想查找所有不在上面列出的任何组中的用户。
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