【问题标题】:System.DirectoryServices.DirectorySearcher works if called from PowerShell but not if called from cmd.exe如果从 PowerShell 调用 System.DirectoryServices.DirectorySearcher 可以工作,但如果从 cmd.exe 调用则不能
【发布时间】:2011-09-04 11:59:59
【问题描述】:

我为 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。

有什么建议吗?

非常感谢。

【问题讨论】:

  • 如果服务器有 SP2 则支持 PowerShell 2.0。见support.microsoft.com/kb/968929
  • 是的,谢谢,我尝试安装 PowerShell 2.0,但它说我需要 .Net Framework 2.0 SP1。我宁愿不必在客户服务器上安装任何东西,但我会记住这是一个选项。
  • 所以我升级到 2.0 看看是否能解决问题。但我仍然得到相同的结果。
  • 我只是从 PowerShell 中调用它。我很少使用 cmd.exe 了。我的评论是现在没有理由使用 v1.0。
  • 好的,我知道了。您是否知道是否有一些上下文或环境设置阻止 DirectorySearcher 获取结果?我的意思是,我没有在任何地方指定 AD 服务器的 IP 地址或凭据,所以从 cmd.exe 调用脚本时是否有可能使用不正确的值?

标签: scripting powershell active-directory cmd powershell-2.0


【解决方案1】:

最后做了两件事:

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

所以命令是这样运行的:

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

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

谢谢。

【讨论】:

    【解决方案2】:

    默认情况下,您在本地 AD i 的根目录上的 $rootEntry 点是您在服务器上运行的,这与当前进程的凭据有关。你没有显示你的过滤器是什么以及你如何使用你的结果。

    这是一个从 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","administrateur@societe.fr","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()
    

    【讨论】:

    • 谢谢。添加了 LDAP 网址和凭据,但仍然存在问题。我需要导入一些模块或一些配置文件吗?我想获取用户组成员信息,所以我使用了这个过滤器 (|(member=$dn)(member=$uid))。搜索后,我只打印组专有名称。
    • 请您给出您在 CMD.EXE 提示符下编写的确切命令
    • 没错,就是这样:>>powershell -command ./script.ps1 "dn" "uid"
    • 我走对了,-file 允许你执行一个 .PS1 文件,它以正确的方式处理参数,这在 PowerShell V1.0 中不能很好地工作 -command 像你一样模拟一个命令在 Powershell 中,但要启动脚本,您最好使用 "invoke-expression" powershell -command "& {invoke-expression '.\script.ps1 \"dn\" \"uid\" '}
    • 感谢您的帮助。我也试试看。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-27
    • 1970-01-01
    • 2013-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多