Results 1 to 3 of 3

Thread: activedirectory get OUs from username

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Norwich, UK
    Posts
    405

    activedirectory get OUs from username

    I need to get the ous that a user belongs to from the username

    is this possible?

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Norwich, UK
    Posts
    405

    Re: activedirectory get OUs from username

    early there. i have managed to drill through ad to find where a particular user is located. however for some reason the fChild.username property is alway nothing. why would this be? looking in active directory the username is there, so why isn't the property picking it up?

    any help would be appreciated

    Code:
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim RootDSE As New DirectoryServices.DirectoryEntry("LDAP://RootDSE")
            Dim DomainDN As String = RootDSE.Properties("DefaultNamingContext").Value
            Dim oOU As New DirectoryServices.DirectoryEntry("LDAP://" & DomainDN)
            Dim oChild As DirectoryServices.DirectoryEntry
            Dim fChild As DirectoryServices.DirectoryEntry
            For Each oChild In oOU.Children
                'MsgBox(oChild.Path)
                If oChild.Name = "OU=Centre" Then
                    Dim sChild As DirectoryServices.DirectoryEntry
                    For Each sChild In oChild.Children
                        If sChild.Name = "OU=General Users" Then
                            Dim tChild As DirectoryServices.DirectoryEntry
                            For Each tChild In sChild.Children
                                For Each fChild In tChild.Children
                                    If fChild.Username = "myusername" Then
                                        MsgBox(tChild.Name & " : " & fChild.Name)
                                    End If
                                Next
                            Next
                        End If
                    Next
                End If
            Next
        End Sub

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Norwich, UK
    Posts
    405

    Resolved Re: activedirectory get OUs from username[RESOLVED]

    got it. don't know why the username property is nothing,

    but found i could use the sAMAccountName property to get it (don't know why)

    Code:
     
       Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim RootDSE As New DirectoryServices.DirectoryEntry("LDAP://RootDSE")
            Dim DomainDN As String = RootDSE.Properties("DefaultNamingContext").Value
            Dim oOU As New DirectoryServices.DirectoryEntry("LDAP://" & DomainDN)
            Dim oChild As DirectoryServices.DirectoryEntry
            Dim fChild As DirectoryServices.DirectoryEntry
            For Each oChild In oOU.Children
                'MsgBox(oChild.Path)
                If oChild.Name = "OU=Centre" Then
                    Dim sChild As DirectoryServices.DirectoryEntry
                    For Each sChild In oChild.Children
                        If sChild.Name = "OU=General Users" Then
                            Dim tChild As DirectoryServices.DirectoryEntry
                            For Each tChild In sChild.Children
                                For Each fChild In tChild.Children
                                    If fChild.Properties("sAMAccountName").Value = "myname" Then
                                        MsgBox(tChild.Name & " : " & fChild.Name)
                                    End If
                                Next
                            Next
                        End If
                    Next
                End If
            Next
        End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width