Solved

Retrieve GUID on all users in Active Directory

Posted on 2014-11-03
2
128 Views
Last Modified: 2015-02-19
Hello,
I currently pull a list of all the Active Directory users on the network.  
What I need now is their GUID.  
The GUID is the same for every user.  So I don't think it's correct.  

Here's the code.
Dim userEntry As DirectoryEntry
userEntry = New DirectoryEntry("LDAP://nicb.org/CN=Users")

Dim ADEntry As New System.DirectoryServices.DirectoryEntry("WinNT://xxx.org")

For Each userEntry In ADEntry.Children
    Dim thisGuid As String
    Dim oGuid As Guid

    If userEntry.SchemaClassName = "User" Then
      oGuid = userEntry.Guid
      thisGuid = oGuid.ToString
      Response.Write(userEntry.Properties("FullName").Value.ToString & " / " & userEntry.Name.ToString & " / " & thisGuid & "<br>")

    End If
Next

I'm using ASP.NET 4.0 VB web app.

JS
0
Comment
Question by:jshesek
2 Comments
 
LVL 70

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 40421121
Use the DirectorySearcher and request the objectGUID attribute.
Dim SearchRoot as New DirectoryEntry()
Dim Searcher As New DirectorySearcher(SearchRoot, "(&(objectClass=user)(objectCategory=person))")
' Searcher.PageSize = 1000
Searcher.PropertiesToLoad.Add("objectGUID")
Searcher.SizeLimit = 10

For Each Result As SearchResult In Searcher.FindAll()
  Dim GuidBytes() As Byte = Result.Properties("objectguid")(0)
  Dim objectGuid As New Guid(GuidBytes)
  
  Response.Write(objectGuid.ToString())
Next

Open in new window

Chris
0
 
LVL 1

Author Comment

by:jshesek
ID: 40421817
Worked really well - Thanks
The code shows where you add in the different properties that you need into the Searcher.
Searcher.PropertiesToLoad.Add("objectGUID")
Searcher.PropertiesToLoad.Add("Name")  

Then you can pull the data out for use
Dim GuidBytes() As Byte = Result.Properties("objectguid")(0)
Dim thisUser As String = Result.Properties("Name")(0)
Dim objectGuid As New Guid(GuidBytes)
If (InStr(thisUser, ",") > 0) Then
      Response.Write(objectGuid.ToString() & " _  " & thisUser & "<br>")
End If

If you know of any good links, for better learning, on this, I'd appreciate it if you would share.
JS
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Last week, our Skyport webinar on “How to secure your Active Directory” (https://www.experts-exchange.com/videos/5810/Webinar-Is-Your-Active-Directory-as-Secure-as-You-Think.html?cid=Gene_Skyport) provided 218 attendees with a step-by-step guide for…
This article outlines the process to identify and resolve account lockout in an Active Directory environment.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

770 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