none
What is the correct format for an LDAP query i.e. a top level OU with a space in its name?

    Question

  • Hi there.

    Thanks to the effort of some people on this forum, i now have a working script. However, at present it's looking at a top level OU with a name of TEST. I tried to alter my LDAP query for our actual OU containing real users, however it threw up an error message:

    Exception calling "FindAll" with "0" argument(s): "There is no such object on the server. " At C:\scripts\LocateUsersNoLogon.ps1:28 char:35 + $colResults = $objSearcher.FindAll <<<< ()  + CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : DotNetMethodException


    I believe and I could be wrong, but the problem lies with the LDAP query because the real/live OU is called "Corp Users". As you can see straight away, there is a space. My LDAP query is below.

    $objOU = New-Object System.DirectoryServices.DirectoryEntry("LDAP://OU=Corp Users,OU=Windows7,dc=company,dc=co,dc=uk") 
    

    When i replace the "OU=Corp Users" and delete the "OU=Windows7" with "OU=TEST" the query runs without a problem. Just to clarify it looks like this:

    $objOU = New-Object System.DirectoryServices.DirectoryEntry("LDAP://OU=TEST,dc=company,dc=co,dc=uk")
    

    If all the code is required (there isn't a lot) then i shall post it. I just don't think it's relevant at the moment.

    Any ideas would be helpful..


    David Bowles - iOS Developer - www.davidjamesbowles.wordpress.com


    • Edited by David Bowles Wednesday, October 26, 2011 2:57 PM spelling mistake
    Wednesday, October 26, 2011 2:53 PM

Answers

  • Spaces in object names do not require any special treatment; the problem is that the OU you specified really doesn't exist at the path specified. If you're not sure where it is, try browsing through your tree using ADExplorer or another tool. Otherwise you could search for it using PowerShell:

    $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
    $objSearcher.SearchRoot = New-Object System.DirectoryServices.DirectoryEntry
    $objSearcher.Filter = "(&(objectCategory=organizationalUnit)(ou=Corp Users))"
    $objSearcher.SearchScope = "Subtree"
    
    $colResults = $objSearcher.FindAll()
    
    foreach ($oResult in $colResults) {
        $oResult.Properties["distinguishedname"]
    }


    Or the one-liner:

    foreach ($oResult in ([AdsiSearcher]"(&(objectCategory=organizationalUnit)(ou=Domain Controllers))").FindAll()) { $oResult.Properties["distinguishedname"] }



    jmh

    • Proposed as answer by Bigteddy Wednesday, October 26, 2011 3:16 PM
    • Edited by Joshua Honig Wednesday, October 26, 2011 3:20 PM
    • Marked as answer by David Bowles Thursday, October 27, 2011 8:38 AM
    • Unmarked as answer by David Bowles Thursday, October 27, 2011 8:42 AM
    • Marked as answer by David Bowles Thursday, October 27, 2011 8:53 AM
    Wednesday, October 26, 2011 3:15 PM

All replies

  • Spaces in object names do not require any special treatment; the problem is that the OU you specified really doesn't exist at the path specified. If you're not sure where it is, try browsing through your tree using ADExplorer or another tool. Otherwise you could search for it using PowerShell:

    $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
    $objSearcher.SearchRoot = New-Object System.DirectoryServices.DirectoryEntry
    $objSearcher.Filter = "(&(objectCategory=organizationalUnit)(ou=Corp Users))"
    $objSearcher.SearchScope = "Subtree"
    
    $colResults = $objSearcher.FindAll()
    
    foreach ($oResult in $colResults) {
        $oResult.Properties["distinguishedname"]
    }


    Or the one-liner:

    foreach ($oResult in ([AdsiSearcher]"(&(objectCategory=organizationalUnit)(ou=Domain Controllers))").FindAll()) { $oResult.Properties["distinguishedname"] }



    jmh

    • Proposed as answer by Bigteddy Wednesday, October 26, 2011 3:16 PM
    • Edited by Joshua Honig Wednesday, October 26, 2011 3:20 PM
    • Marked as answer by David Bowles Thursday, October 27, 2011 8:38 AM
    • Unmarked as answer by David Bowles Thursday, October 27, 2011 8:42 AM
    • Marked as answer by David Bowles Thursday, October 27, 2011 8:53 AM
    Wednesday, October 26, 2011 3:15 PM
  • ([adsisearcher]"(&(objectclass=organizationalunit))").FindAll() - show all ous with their paths.
    • Proposed as answer by Bigteddy Wednesday, October 26, 2011 3:16 PM
    Wednesday, October 26, 2011 3:15 PM
  • As noted, spaces require no special handling in distinguished names (unless they are the leading or trailing character in a component name, such as the name of an OU). I'm just guessing, but perhaps a space is missing in "ou=Windows7". Also, the string is not case sensitive.

     


    Richard Mueller - MVP Directory Services
    Wednesday, October 26, 2011 4:53 PM
  • Hi guys, thanks for the replies. I have downloded AD Explorer, i can confirm that under DC=company,DC=co,DC=uk there is an entry called OU=Corp Users

    Under OU=Corp Users I can confirm that there isn't an entry for OU=Windows7 (there isn't a space i double checked). So I've now got the task of trying to locate it!

    So thank you for your help once again!


    David Bowles - iOS Developer - www.davidjamesbowles.wordpress.com
    Thursday, October 27, 2011 8:38 AM
  • Hi please scrub my last post, i've just looked for the OU called Windows7 in AD Explorer and it is under the OU Corp Users.

    So I am now more confused than ever.

    I'm a bit reluctant to post a screen shot, obviously!

    Any ideas?


    David Bowles - iOS Developer - www.davidjamesbowles.wordpress.com
    Thursday, October 27, 2011 8:43 AM
  • hi guys, i resolved the issue, I had my OUs in the wrong order!!!

    Thank you for your responses once again.

    David.


    David Bowles - iOS Developer - www.davidjamesbowles.wordpress.com
    Thursday, October 27, 2011 8:54 AM
  • hi guys, i resolved the issue, I had my OUs in the wrong order!!!

    Bingo! Yep, you have to list the name tree in reverse order in a distinguished name. (Most specific to least specific).


    jmh
    Friday, October 28, 2011 11:04 AM