Solved

Retrieve GUID on all users in Active Directory

Posted on 2014-11-03
2
134 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
[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 Comments
 
LVL 71

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This article explains the steps required to use the default Photos screensaver to display branding/corporate images
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

729 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