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

PowerShell DirectorySearcher返回部分数据后报‘More data is available’如何获取剩余数据?

问题:如何获取AD查询中剩余的数据?

使用PowerShell查找所有拥有直接下属(direct reports)的用户时,查询返回部分数据后报错“有更多数据可用”,请问如何获取剩余的数据?

原查询代码

$objDomain = New-Object System.DirectoryServices.DirectoryEntry("GC://DC=prod,DC=org,DC=example,DC=com")

$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.SearchScope = "Subtree"
$objSearcher.ReferralChasing = [DirectoryServices.ReferralChasingOption]::None

$objSearcher.Filter = "(&(objectClass=user)(objectCategory=person)(customAttribute=*123456*)(directReports=*))"

$objSearcher.PropertiesToLoad.AddRange("mail".Split(","))

$colResults = $objSearcher.FindAll()

foreach ($objResult in $colResults)
{
    Write-Output $objResult.Properties.Item("mail")
}

输出结果及报错信息

user1@org.example.com
user2@org.example.com
user3@verizonconnect.com
user4@org.example.com
user5@verizonwireless.com
有更多数据可用。
At line:15 char:10
+ foreach ($objResult in $colResults)
+          ~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], DirectoryServicesCOMException
    + FullyQualifiedErrorId : System.DirectoryServices.DirectoryServicesCOMException

解决方案

这个错误是因为FindAll()返回的SearchResultCollection在遍历中遇到数据截断或分页未处理完全导致的,以下是几种可行的解决方法:

1. 优化分页配置并捕获异常继续遍历

手动获取枚举器,在捕获到“有更多数据可用”的特定异常后,继续遍历剩余结果,同时记得释放资源:

$objDomain = New-Object System.DirectoryServices.DirectoryEntry("GC://DC=prod,DC=org,DC=example,DC=com")

$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.ServerPageTimeLimit = [TimeSpan]::FromSeconds(30) # 延长分页超时时间
$objSearcher.SearchScope = "Subtree"
$objSearcher.ReferralChasing = [DirectoryServices.ReferralChasingOption]::None

$objSearcher.Filter = "(&(objectClass=user)(objectCategory=person)(customAttribute=*123456*)(directReports=*))"
$objSearcher.PropertiesToLoad.AddRange("mail".Split(","))

$colResults = $objSearcher.FindAll()
$enumerator = $colResults.GetEnumerator()

try {
    while ($enumerator.MoveNext()) {
        Write-Output $enumerator.Current.Properties.Item("mail")
    }
}
catch [System.DirectoryServices.DirectoryServicesCOMException] {
    # 匹配"有更多数据可用"的错误代码0x800700EA
    if ($_.Exception.HResult -eq 0x800700EA) {
        # 继续遍历剩余数据
        while ($enumerator.MoveNext()) {
            Write-Output $enumerator.Current.Properties.Item("mail")
        }
    }
    else {
        # 非目标异常直接抛出
        throw $_
    }
}
finally {
    # 强制释放资源,避免内存泄漏
    $colResults.Dispose()
}

2. 改用ActiveDirectory模块简化查询(环境支持时优先)

如果你的PowerShell环境安装了ActiveDirectory模块,Get-ADUser会自动处理分页和数据截断问题,代码更简洁:

Get-ADUser -Filter {
    objectClass -eq 'user' -and
    objectCategory -eq 'person' -and
    customAttribute -like '*123456*' -and
    directReports -ne $null
} -Properties mail -SearchBase "DC=prod,DC=org,DC=example,DC=com" -SearchScope Subtree |
Select-Object -ExpandProperty mail

关键注意事项

  • 全局编录(GC://)查询可能限制部分属性的完整访问,如果需要精准的directReports数据,建议直接连接到域控制器(LDAP://DC=prod,DC=org,DC=example,DC=com)而非全局编录。
  • 无论哪种方法,都要确保释放SearchResultCollection的资源,避免内存泄漏。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 23:42:52