You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改ADSI Searcher命令以获取安全组的次要所有者?

修改方案1:查询特定用户作为主要/次要所有者的所有安全组

AD中安全组的主要所有者对应属性ManagedBy,次要所有者对应多值属性msDS-SecondaryOwner。只需修改LDAP过滤条件,用逻辑或同时匹配这两个属性即可。

修改后的完整命令

# 用户的Distinguished Name
$DN = "CN=用户名,OU=用户组,DC=domain,DC=com"

# 查询该用户作为主要或次要所有者的所有安全组
$searcher = [ADSISearcher]"(&(objectCategory=group)(objectClass=group)(|(ManagedBy=$DN)(msDS-SecondaryOwner=$DN)))"
# 指定要返回的属性,按需调整
$searcher.PropertiesToLoad.AddRange(@("samaccountname", "managedby", "msds-secondaryowner", "name"))

$groups = $searcher.FindAll()

# 处理结果并输出到文件
$results = foreach ($group in $groups) {
    [PSCustomObject]@{
        GroupName           = $group.Properties["name"][0]
        GroupSamAccountName = $group.Properties["samaccountname"][0]
        PrimaryOwnerDN      = $group.Properties["managedby"][0]
        SecondaryOwnerDNs   = $group.Properties["msds-secondaryowner"] -join "; "
    }
}

$results | Export-Csv -Path "C:\users\你的路径\GroupOwners.csv" -NoTypeInformation -Encoding UTF8

关键说明

  • 过滤条件新增(|(ManagedBy=$DN)(msDS-SecondaryOwner=$DN)),表示匹配主要所有者是该用户或次要所有者包含该用户的组
  • 添加objectClass=group确保只返回安全组(默认objectCategory=group会包含通讯组,按需可移除)
  • 用PSCustomObject整理输出字段,方便查看和导出;如果需要解析所有者的用户名,可以额外查询AD转换DN为名称

获取所有安全组的主要/次要所有者(全局查询)

如果需要遍历环境中所有安全组并提取所有者信息,使用以下命令:

$searcher = [ADSISearcher]"(&(objectCategory=group)(objectClass=group))"
$searcher.PropertiesToLoad.AddRange(@("samaccountname", "name", "managedby", "msds-secondaryowner"))

$allGroups = $searcher.FindAll()

$allResults = foreach ($group in $allGroups) {
    # 解析主要所有者名称(可选)
    $primaryOwnerName = if ($group.Properties["managedby"]) {
        $ownerSearcher = [ADSISearcher]"(&(distinguishedname=$($group.Properties["managedby"][0])))"
        $ownerSearcher.PropertiesToLoad.Add("name")
        $ownerSearcher.FindOne().Properties["name"][0]
    } else { "无" }

    # 解析次要所有者名称(可选)
    $secondaryOwnerNames = if ($group.Properties["msds-secondaryowner"]) {
        ($group.Properties["msds-secondaryowner"] | ForEach-Object {
            $ownerSearcher = [ADSISearcher]"(&(distinguishedname=$_))"
            $ownerSearcher.PropertiesToLoad.Add("name")
            $ownerSearcher.FindOne().Properties["name"][0]
        }) -join "; "
    } else { "无" }

    [PSCustomObject]@{
        GroupName           = $group.Properties["name"][0]
        GroupSamAccountName = $group.Properties["samaccountname"][0]
        PrimaryOwner        = $primaryOwnerName
        SecondaryOwners     = $secondaryOwnerNames
    }
}

$allResults | Export-Csv -Path "C:\users\你的路径\AllGroupOwners.csv" -NoTypeInformation -Encoding UTF8

关键说明

  • 遍历所有安全组,自动解析所有者DN对应的用户名,无需手动转换
  • 输出结果包含组名、组账号名、主要所有者、次要所有者,更直观

内容的提问来源于stack exchange,提问作者RyanHindley1992

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 15:21:01