我正在使用 PowerShell 查找所有拥有直接下属的用户。该查询似乎有效,因为它返回了一些数据,但随后出现错误并显示一条消息。
如何获取其余数据?
$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")
}
这是最后带有错误的输出:
[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
这可能有效,但我无法测试它,因为我从未见过此错误。
互联网上记录的解决方案(或者实际上是一种解决方法)是使用如下所示的 app.config 文件:
<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 应用程序默认使用 app.config 或 web.config 文件,但 PowerShell 当然不使用。但是使用 this answer 看起来你可以告诉 PowerShell 使用配置文件,如下所示:
[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $config_path)
其中 $config_path
是 app.config 文件的路径。