开发者

System.DirectoryServices.DirectorySearcher works if called from PowerShell but not if called from cmd.exe

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.


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","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()


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜