Solved

Retrieve GUID on all users in Active Directory

Posted on 2014-11-03
2
133 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
This article shows the method of using the Resultant Set of Policy Tool to locate Group Policy that applies a particular setting.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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