System.DirectoryServices.DirectorySearcher 如果从 PowerShell 调用则有效,但如果从 cmd.exe 调用则无效
我为 PowerShell 1.0(现在使用 2.0)编写了一个脚本,用于在我的 Active Directory 上执行搜索。代码如下:
$filter = "some filter"
$rootEntry = New-Object System.DirectoryServices.DirectoryEntry
$searcher = New-Object System.DirectoryServices.DirectorySearcher
$searcher.SearchRoot = $rootEntry
$searcher.Filter = $filter
$searcher.SearchScope = "Subtree"
$colResults = $searcher.FindAll()
调用 DirectorySearcher 实例的 FindAll() 方法后,我打印结果以查看得到的结果。
问题是,如果我启动 PowerShell.exe 并在提示符下调用脚本,我就可以看到结果。但是,如果我尝试使用相同的过滤器使用 cmd.exe 调用它,我看不到任何结果。 FindAll() 返回一个空结果集。
我在 Windows 2003 Server 上运行它。它没有附带 PowerShell 1.0,因此我下载了它并将其安装在服务器上。它确实有.Net Framework 2.0。
有什么建议吗?
多谢。
I wrote an script for PowerShell 1.0 (now using 2.0) that executes a search on my Active Directory. The code is the following:
$filter = "some filter"
$rootEntry = New-Object System.DirectoryServices.DirectoryEntry
$searcher = New-Object System.DirectoryServices.DirectorySearcher
$searcher.SearchRoot = $rootEntry
$searcher.Filter = $filter
$searcher.SearchScope = "Subtree"
$colResults = $searcher.FindAll()
After calling FindAll() method of the DirectorySearcher instance, I print the results to see what I got.
The thing is, if I start PowerShell.exe and call the script on the prompt I'm able to see results. But if I try to call it using cmd.exe using the same filter I don't see any results. FindAll() returns an empty result set.
I'm running this on a Windows 2003 Server. It did not came with PowerShell 1.0 so I downloaded it and installed it on the server. It does have .Net Framework 2.0.
Any suggestions?
Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
默认情况下,您的 $rootEntry 点位于本地 AD 的根目录上,您正在服务器上运行,并且具有当前进程的凭据。您不会显示您的过滤器是什么以及您如何使用您的结果。
以下是来自 PowerShell 的 ADSI 搜索的小示例
By defaul your $rootEntry point on the root of you local AD i you are running on a server, and this with the credetial of the current process. you don't show what is your filter and how you use your result.
Here is a small sample of an ADSI search from PowerShell
最终通过执行两件事使其正常工作:
所以命令是这样运行的:
我不确定 -File 和 -Command 选项之间有什么区别(有人吗?),但它有效。
谢谢。
Finally got it working by doing two things:
So the command was run like this:
I'm not sure what the difference between the -File and -Command options are (does anyone?) but it worked.
Thanks.