Below .NET function is used to get the user groups from Active Directory.
To use the below function in your project, you need to add the reference for System.DirectoryServices
Public Function GetUserGroupMembership(ByVal strUser As String) As StringCollection Dim groups As New StringCollection()
Try
Dim obEntry As New DirectoryServices.DirectoryEntry("LDAP://domain/DC=domain,DC=com")
Dim srch As New DirectoryServices.DirectorySearcher(obEntry, "(sAMAccountName=" & strUser & ")")
Dim res As DirectoryServices.SearchResult = srch.FindOne()
If res IsNot Nothing Then
Dim obUser As New DirectoryServices.DirectoryEntry(res.Path)
' Invoke Groups method.
Dim obGroups As Object = obUser.Invoke("Groups")
For Each ob As Object In DirectCast(obGroups, IEnumerable)
' Create object for each group.
Dim obGpEntry As New DirectoryServices.DirectoryEntry(ob)
groups.Add(obGpEntry.Name)
Next
End If
Catch ex As Exception 'Trace.Write(ex.Message)
End Try
Return groups
End Function
Above function can be called as :
Dim Groups As StringCollection = GetUserGroupMembership(username.text)
"Scripting" is basically giving sequential instructions to computer for doing specific things rather than doing manually. Sometimes we need to write a small and quick vbscript (.vbs), javascript(.js), batch file(.bat) or SQL script(.sql) to perform a short task, but we lack information or get confuse on where to start or which scrpit to write. In my BLOG, I will try to cover as many different scripts as possible that will be useful to everyone.
No comments:
Post a Comment