System.DirectoryServices.DirectorySearcher 如果从 PowerShell 调用则有效,但如果从 cmd.exe 调用则无效

发布于 2024-11-10 06:11:58 字数 691 浏览 6 评论 0原文

我为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半衬遮猫 2024-11-17 06:11:58

默认情况下,您的 $rootEntry 点位于本地 AD 的根目录上,您正在服务器上运行,并且具有当前进程的凭据。您不会显示您的过滤器是什么以及您如何使用您的结果。

以下是来自 PowerShell 的 ADSI 搜索的小示例

Clear-Host
# ADSI Bind with current process credentials
#$dn = [adsi] "LDAP://192.168.30.200:389/dc=dom,dc=fr"
# ADSI Bind with specific credentials
$dn = New-Object System.DirectoryServices.DirectoryEntry ("LDAP://192.168.183.138:389/dc=societe,dc=fr","[email protected]","test.2011")


# Look for users

$Rech = new-object System.DirectoryServices.DirectorySearcher($dn)
$rc = $Rech.filter = "((objectCategory=person))"
$rc = $Rech.SearchScope = "subtree"
$rc = $Rech.PropertiesToLoad.Add("distinguishedName");
$rc = $Rech.PropertiesToLoad.Add("sAMAccountName");  
$rc = $Rech.PropertiesToLoad.Add("ipphone");  
$rc = $Rech.PropertiesToLoad.Add("telephoneNumber");
$rc = $Rech.PropertiesToLoad.Add("memberOf");
$rc = $Rech.PropertiesToLoad.Add("distinguishedname");
$rc = $Rech.PropertiesToLoad.Add("physicalDeliveryOfficeName"); # Your attribute

$liste = $Rech.findall()

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

Clear-Host
# ADSI Bind with current process credentials
#$dn = [adsi] "LDAP://192.168.30.200:389/dc=dom,dc=fr"
# ADSI Bind with specific credentials
$dn = New-Object System.DirectoryServices.DirectoryEntry ("LDAP://192.168.183.138:389/dc=societe,dc=fr","[email protected]","test.2011")


# Look for users

$Rech = new-object System.DirectoryServices.DirectorySearcher($dn)
$rc = $Rech.filter = "((objectCategory=person))"
$rc = $Rech.SearchScope = "subtree"
$rc = $Rech.PropertiesToLoad.Add("distinguishedName");
$rc = $Rech.PropertiesToLoad.Add("sAMAccountName");  
$rc = $Rech.PropertiesToLoad.Add("ipphone");  
$rc = $Rech.PropertiesToLoad.Add("telephoneNumber");
$rc = $Rech.PropertiesToLoad.Add("memberOf");
$rc = $Rech.PropertiesToLoad.Add("distinguishedname");
$rc = $Rech.PropertiesToLoad.Add("physicalDeliveryOfficeName"); # Your attribute

$liste = $Rech.findall()
倾城花音 2024-11-17 06:11:58

最终通过执行两件事使其正常工作:

  1. 升级到 PowerShell 2.0。
  2. 使用 -File 选项运行。

所以命令是这样运行的:

>>powershell -file ./script.ps1 "dn" "uid"

我不确定 -File 和 -Command 选项之间有什么区别(有人吗?),但它有效。

谢谢。

Finally got it working by doing two things:

  1. Upgrade to PowerShell 2.0.
  2. Run with -File option.

So the command was run like this:

>>powershell -file ./script.ps1 "dn" "uid"

I'm not sure what the difference between the -File and -Command options are (does anyone?) but it worked.

Thanks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文