PowerShell DirectorySearcher returns some data and then errors with "More data is available" -- how Can I get the more data?

64 Views Asked by At

I am using PowerShell to find all users that have direct reports. The query seems to work in that it returns some data, but then it errors with a message.

How can I get the rest of the data?

$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")
}

This is the output with the error at the end:

[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
More data is available.
At line:15 char:10
+ foreach ($objResult in $colResults)
+          ~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], DirectoryServicesCOMException
    + FullyQualifiedErrorId : System.DirectoryServices.DirectoryServicesCOMException
1

There are 1 best solutions below

0
Gabriel Luci On BEST ANSWER

This might work, but I can't test it since I've never seen this error.

A solution (or really, a workaround) documented around the internet is to use an app.config file that looks like this:

<configuration>
<configSections>
<section name="system.directoryservices" 
    type="System.DirectoryServices.SearchWaitHandler, 
    System.DirectoryServices, Version=2.0.0.0, Culture=neutral, 
    PublicKeyToken=b03f5f7f11d50a3a" /> 
</configSections>
<system.directoryservices>
    <DirectorySearcher waitForPagedSearchData="true" /> 
</system.directoryservices>
</configuration>

.NET applications use an app.config or web.config file by default, but PowerShell doesn't of course. But using this answer it looks like you can tell PowerShell to use a config file, like this:

[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $config_path)

Where $config_path is the path to your app.config file.

You may or may not need to change the version in the config file to 4.0.0.0.