【问题标题】:Check if the user is a member of a list of AD groups检查用户是否是 AD 组列表的成员
【发布时间】:2018-02-27 22:37:09
【问题描述】:
$groups = 'group1', 'group2'....

我需要检查用户是否在特定的 AD 组中,如果不是,则回显组名;我可以在管道中进行吗?

我用谷歌搜索了很多东西,但找不到任何东西,也许我的英文谷歌搜索太糟糕了:)。

$groups |
    Get-QADGroupMember |
    Get-QADUser -SamAccountName 'lalala' | ForEach-Object {
        if ($_.SamAccountName -ne $null) {
            Write-Host "ok"
        } else {
            Write-Host 'not ok'
        }
    }

如何显示:not ok. user is not ingroup_name

【问题讨论】:

  • 您有一个组列表。用户应该是其中哪个成员?任何?全部?一个具体的?没有?另外,我建议从另一端开始:获取用户所属的组,然后检查列表是否包含相关组。
  • 这里只是一个反问,在理想情况下,一个组通常包含比单个用户更多的成员。话虽这么说,获取每个用户的组并检查他是否是某个组的成员会比反过来更快吗?只是问... :)

标签: powershell active-directory quest


【解决方案1】:

问题是你为什么要使用管道,而只是循环遍历结果如此简单?

检查用户是否是组列表的成员:

$user = "TestUsername"
$groups = 'Domain Users', 'Domain Admins'

foreach ($group in $groups) {
    $members = Get-ADGroupMember -Identity $group -Recursive | Select -ExpandProperty SamAccountName

    If ($members -contains $user) {
        Write-Host "$user is a member of $group"
    } Else {
        Write-Host "$user is not a member of $group"
    }
}

对于多个用户:

$users = "TestUsername1", "TestUsername2", "TestUsername3"
$groups = 'Domain Users', 'Domain Admins'

foreach ($user in $users) {
    foreach ($group in $groups) {
        $members = Get-ADGroupMember -Identity $group -Recursive | Select -ExpandProperty SamAccountName

        If ($members -contains $user) {
            Write-Host "$user is a member of $group"
        } Else {
            Write-Host "$user is not a member of $group"
        }
    }
}

【讨论】:

  • 我遇到了错误The term 'Get-ADGroupMember' is not recognized as the name of a cmdlet。我需要导入什么模块才能使其工作? -edit- 在stackoverflow.com/a/17548616/602585找到答案
  • @PSaR 虽然您的编辑具有建设性,但它更适合作为单独的答案。这是我的一个旧答案,我也不会再这样做了,因为它效率不高。
【解决方案2】:

如果您的服务器上没有安装 Active Directory PowerShell 功能,您可以使用此方法。在这里,我正在检查域组是否是服务器上本地管理员组的一部分,但是如果要检查用户是否属于某个组,您只需将 GroupPrincipal 更改为 UserPrincipal 并提供用户名即可。此外,如果该组是域组,则对两个 FindByIdentity 调用使用 $domainContext

function Test-DomainGroupIsMemberOfLocalAdministrators([string] $domainName, [string] $domainGroupName)
{
    Add-Type -AssemblyName 'System.DirectoryServices.AccountManagement'
    $domainContext = [System.DirectoryServices.AccountManagement.PrincipalContext]::new([System.DirectoryServices.AccountManagement.ContextType]::Domain, $domainName)
    $localMachineContext = [System.DirectoryServices.AccountManagement.PrincipalContext]::new([System.DirectoryServices.AccountManagement.ContextType]::Machine)
    $domainGroup = [System.DirectoryServices.AccountManagement.GroupPrincipal]::FindByIdentity($domainContext, $domainGroupName)
    $localAdministratorsGroup = [System.DirectoryServices.AccountManagement.GroupPrincipal]::FindByIdentity($localMachineContext, "Administrators")

    if($domainGroup -ne $null)
    {
        if ($domainGroup.IsMemberOf($localAdministratorsGroup))
        {
            return $true
        }
    }
    return $false
}

【讨论】:

    猜你喜欢
    • 2023-04-11
    • 1970-01-01
    • 2019-04-30
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    • 2019-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多