I need to get the ous that a user belongs to from the username
is this possible?
Printable View
I need to get the ous that a user belongs to from the username
is this possible?
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
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