GUID of a group in active directory

How do I get the guid of a group in active directory - I am using this code to get the group - can it be modified to get the string guid or guid as well as the group
    Public Shared Function GetAllGroups() As DataSet
       
        Dim dsGroup As New DataSet()
        Dim dirEntry As DirectoryEntry = New DirectoryEntry("LDAP://OU=TRGET OU,OU=AN OU,DC=COMPANY,DC=com", "USER", "PASS")
        'create instance fo the direcory searcher
        Dim dirSearch As New DirectorySearcher()
        'set the search filter
        dirSearch.SearchRoot = dirEntry
        'Set the filter
        dirSearch.Filter = "(objectClass=group)" 
        'find the first instance
        Dim searchResults As SearchResultCollection = dirSearch.FindAll()
        'Create a new table object within the dataset
        Dim dtGroup As DataTable = dsGroup.Tables.Add("Groups")
        dtGroup.Columns.Add("GroupName")
        'if there are results (there should be some!!), then convert the results
        'into a dataset to be returned.
        If searchResults.Count > 0 Then
            'iterate through collection and populate the table with
            'the Group Name
            For Each Result As SearchResult In searchResults
                'set a new empty row
                Dim drGroup As DataRow = dtGroup.NewRow()
                'populate the column
                drGroup("GroupName") = Result.Properties("cn")(0)
                'append the row to the table of the dataset
                dtGroup.Rows.Add(drGroup)
            Next
        End If
        Return dsGroup
    End Function

Open in new window

LVL 6
r3nderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

acbxyzCommented:
Can't test it here, but this should work.
Add another column to the DataSet:
        dtGroup.Columns.Add("GroupGUID")
and populate the value:
                drGroup("GroupGUID") = Result.Properties("objectGUID")(0)


You can also use objectSid if you want.
0
BuggyCoderCommented:
i found a very good(complete) article in AD, kindly see and let me know if it helps:-

http://www.codeproject.com/Articles/18102/Howto-Almost-Everything-In-Active-Directory-via-C
0
r3nderAuthor Commented:
I keep getting System.Bytein the label i created to check to see if I got it
Why cant I get the GUID
    Public Shared Function GetAllGroups() As DataSet
        Dim dsGroup As New DataSet()
        Dim dirEntry As DirectoryEntry = New DirectoryEntry("LDAP://AD.local", "USER", "PASS")
        'create instance fo the direcory searcher
        Dim dirSearch As New DirectorySearcher()
        'set the search filter
        dirSearch.SearchRoot = dirEntry
        'Set the filter
        dirSearch.Filter = "(objectClass=group)"
        'find the first instance
        Dim searchResults As SearchResultCollection = dirSearch.FindAll()
        'Create a new table object within the dataset
        Dim dtGroup As DataTable = dsGroup.Tables.Add("Groups")
        dtGroup.Columns.Add("GroupName")
        dtGroup.Columns.Add("GroupGUID").ToString()
        'if there are results (there should be some!!), then convert the results
        'into a dataset to be returned.
        If searchResults.Count > 0 Then
            'iterate through collection and populate the table with
            'the Group Name
            For Each Result As SearchResult In searchResults
                'set a new empty row
                Dim drGroup As DataRow = dtGroup.NewRow()
                'populate the column
                drGroup("GroupName") = Result.Properties("cn")(0)
                drGroup("GroupGUID") = Result.Properties("objectGUID")(0)
                'append the row to the table of the dataset
                dtGroup.Rows.Add(drGroup)
            Next
        End If
        Return dsGroup

    End Function

Open in new window

    Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        dsGroups = GetAllGroups()
        Me.ListBox1.DataSource = dsGroups.Tables("Groups")
        Me.ListBox1.DisplayMember = "GroupName"
        ListBox1.ValueMember = "GroupGUID"
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Label1.Text = ListBox1.SelectedValue.ToString
    End Sub
End Class

Open in new window

0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

r3nderAuthor Commented:
Mabe I should say is how do I  convert System.Byte[] to the GUID in string format
0
BuggyCoderCommented:
try either of these:-
Result.Properties("objectGUID")(0).ToString()

or

Encoding.Ascii.GetString(byteArray);
0
r3nderAuthor Commented:
the first one showed System.Bytes[] in the label

the encoding of the text in the lablel thew an error Unable to cast object of type 'System.String' to type 'System.Byte[]'.
0
r3nderAuthor Commented:
I even tried this (see attachment)
screenshot.jpg
0
BuggyCoderCommented:
can you share your byte array???
0
BuggyCoderCommented:
can you try this:-
Dim b = DirectCast(result.Properties("objectGuid")(0), Byte())
Return New Guid(b)

Open in new window


see this for reference:-
http://www.extensionmethod.net/Details.aspx?ID=299
0
r3nderAuthor Commented:
This is everything
Imports System.Data
Imports System.DirectoryServices
Imports System.Data.SqlClient
Imports System.Text

Public Class Form2
    Dim ds As DataSet
    Dim dsGroups As DataSet
Public Shared Function ConvertGuidToOctectString(objectGuid As String) As String
        Dim guid As System.Guid = New Guid(objectGuid)
        Dim byteGuid As Byte() = guid.ToByteArray()
        Dim queryGuid As String = ""
        For Each b As Byte In byteGuid
            queryGuid += "\" & b.ToString("x2")
        Next
        Return queryGuid
    End Function


        
    Public Shared Function GetAllGroups() As DataSet
        Try
            Dim dsGroup As New DataSet()
            Dim dirEntry As DirectoryEntry = New DirectoryEntry("LDAP://DC", "USER", "PASS")
            'create instance fo the direcory searcher
            Dim dirSearch As New DirectorySearcher()
            'set the search filter
            dirSearch.SearchRoot = dirEntry
            'Set the filter
            dirSearch.Filter = "(objectClass=group)"
            'find the first instance
            Dim searchResults As SearchResultCollection = dirSearch.FindAll()
            'Create a new table object within the dataset
            Dim dtGroup As DataTable = dsGroup.Tables.Add("Groups")
            dtGroup.Columns.Add("GroupName")
            dtGroup.Columns.Add("GroupGUID")
            'if there are results (there should be some!!), then convert the results
            'into a dataset to be returned.
            If searchResults.Count > 0 Then
                'iterate through collection and populate the table with
                'the Group Name
                For Each Result As SearchResult In searchResults
                    'set a new empty row
                    Dim drGroup As DataRow = dtGroup.NewRow()
                    'populate the column
                    drGroup("GroupName") = Result.Properties("cn")(0)
                    'drGroup("GroupGUID") = Result.Properties("objectGUID")(0)
                    drGroup("GroupGUID") = DirectCast(Result.Properties("objectGuid")(0), Byte())

                    'append the row to the table of the dataset
                    dtGroup.Rows.Add(drGroup)
                Next
            End If
            Return dsGroup
        Catch E As Exception
            MessageBox.Show(E.Message)
        End Try

    End Function
    Public Shared Function getSQL() As DataSet
        Try
            Dim connString As String = "server=SQLEXPRESS;database=DB; user=USER; password=PASS;trusted_connection=yes"
            Dim conn As New SqlConnection(connString)
            conn.Open()
            Dim strSQL As String = "SELECT Country,Value FROM Country"
            Dim da As New SqlDataAdapter(strSQL, conn)
            Dim ds As New DataSet
            da.Fill(ds, "Country")
            'With cbUserRegion
            '    .DataSource = ds.Tables("Country")
            '    .DisplayMember = "Country"
            '    .ValueMember = "Value"
            '    .SelectedIndex = 0
            'End With

            'With cbCountry
            '    .DataSource = ds.Tables("Country")
            '    .DisplayMember = "Country"
            '    .ValueMember = "value"
            '    .SelectedIndex = 0
            'End With
            conn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Function

    Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        dsGroups = GetAllGroups()

        Me.ListBox1.DataSource = dsGroups.Tables("Groups")
        Me.ListBox1.DisplayMember = "GroupName"
        ListBox1.ValueMember = "GroupGUID"
    End Sub

    'Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

    '    Dim tGuid As Guid
    '    ' tGuid = New Guid(CType(tItem.Properties.Item("objectGUID").Value, System.Byte()))
    '    tGuid = New Guid(CType(ListBox1.SelectedValue, System.Byte()))
    '    Label1.Text = tGuid.ToString
    'End Sub

    Public Function ConvertDNtoGUID(objectDN As String) As String
        'Removed logic to check existence first

        Dim directoryObject As New DirectoryEntry(objectDN)
        Return directoryObject.Guid.ToString()
    End Function

    Private Sub ListBox1_MouseClick(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseClick

       

        Label1.Text = ListBox1.SelectedValue.ToString
 

    End Sub
    Private Function Byte2Str(ByVal gByte() As Byte) As String
        Dim X As Integer
        Dim gTmp As String = ""
        For X = 0 To gByte.Length - 1
            gTmp = gTmp & Chr(gByte(X))
        Next
        Return (gTmp)
    End Function


End Class

Open in new window

It still returns system.byte[]
0
r3nderAuthor Commented:
Why didnt anyone say there was a GUID class
I found the solution
For Each Result As SearchResult In searchResults
                    'set a new empty row
                    Dim drGroup As DataRow = dtGroup.NewRow()
                    'populate the column
                    drGroup("GroupName") = Result.Properties("cn")(0)
                    Dim GuidByteAr() As Byte = Result.Properties("objectGUID")(0)
                    Dim TestGuid As New Guid(GuidByteAr)
                    drGroup("GroupGUID") = TestGuid.ToString()
                    'drGroup("GroupGUID") = DirectCast(Result.Properties("objectGuid")(0), Byte())
                    'append the row to the table of the dataset
                    dtGroup.Rows.Add(drGroup)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
r3nderAuthor Commented:
Thanks your your help on this
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Active Directory

From novice to tech pro — start learning today.