[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Active Directory Search not working

Posted on 2014-03-13
3
Medium Priority
?
298 Views
Last Modified: 2014-03-14
Using VB.NET

Trying to retreive the 'givenname' and 'SN' from Active Directory using the following code:

<snip>
oSearcher.Filter = "(&(objectCategory=user)(objectClass=user)(samaccountname=" & pUsername & "))"

        oSearcher.PropertiesToLoad.AddRange(New String() {"givenName", "SN"}
        Try
            oResults = oSearcher.FindAll

            For Each oResult In oResults
                returnVal = oResult.Properties("givenname").ToString()
            Next

Open in new window


but 'returnVal' is set to: "System.DirectoryServices.ResultPropertyValueCollection" and not the givenname

The 'DirectoryEntry' for my DC etc. is correct as I'm using the same thing for another query that works just fine and is very similar to this one.

I get no actual error, just not gettng the data I need

What am I missing?
0
Comment
Question by:BrianFord
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 2000 total points
ID: 39928740
Well I can't seem to find a useful example in the documentation but this works for me:
returnVal = oResult.Properties("givenname")(0).ToString

Open in new window

So add the (0) because the property value is actually a collection of values.

In some cases you need to be aware that a property may have no value or more than 1 value.
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 39928756
Ah, now also found a more generic example here, should that be applicable (wanting to show all properties found):
        Dim propKey As String
        For Each propKey In oResult.Properties.PropertyNames
            ' Display each of the values for the property 
            ' identified by the property name.
            Dim prop As Object
            For Each prop In oResult.Properties(propKey)
                Console.WriteLine("{0}:{1}", propKey, [prop].ToString())
            Next prop
        Next propKey

Open in new window

0
 

Author Closing Comment

by:BrianFord
ID: 39928892
Adding the zero did the trick :)
Thanks very much for your help
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Always backup Domain, SYSVOL etc.using processes according to Microsoft Best Practices. This is meant as a disaster recovery process for small environments that did not implement backup processes and did not run a secondary domain controller that ne…
This process allows computer passwords to be managed and secured without using LAPS. This is an improvement on an existing process, enhanced to store password encrypted, instead of clear-text files within SQL
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question