• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 445
  • Last Modified:

VB Script Needed to query user information from AD

I am needing to gain a list of users in a specific security or distribution group.  For example the query needs to provide the following information in a csv file.

first name,last name,username,email address

It would be nice if when the script is executed it would pop up a window asking for the specific group name to query.
1 Solution
You'll need to change the domain value.
OM Gang

'Script begins here
'writes the names and e-mail addresses of the members of the domain group entered by user
'to a csv file on the users desktop

Dim objGroup, objUser, objFSO, objFile, objShell, objObject
Dim strDomain, strGroup, Domain, Group, strUserName, strEmail, strLName, strFName, strUserProfilePath, strPhone

      'local domain
strDomain = "MyDomain"

      'prompt for the name of the group whose members we want to export
strGroup = InputBox ("Enter AD group you want to export", "Group Name?")

      'create file system and shell objects
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")

      'get path to logged on users profile so we can write to their desktop directory
strUserProfilePath = objShell.ExpandEnvironmentStrings("%UserProfile%")

      'create destination text file in users desktop directory
      'name it the same as the group being queried
Set objFile = objFSO.CreateTextFile(strUserProfilePath & "\Desktop\" & strGroup & ".csv")

      'get the specified group in the specified domain
Set objGroup = GetObject("WinNT://" & strDomain & "/" & strGroup & ",group")

      'iterate through the user objects in the group
      'ignore non-user objects such as nested groups
For Each objUser In objGroup.Members
      strUserName = objUser.Name
            'set ADODB connection to LDAP so we can query for this user object and retrieve field values
      Set RootDSE = GetObject("LDAP://RootDSE")
      searchRoot = RootDSE.Get("defaultNamingContext")

      Set objConnection = CreateObject("ADODB.Connection")
      Set objCommand = CreateObject("ADODB.Command")
      objConnection.Provider = "ADsDSOObject"
      objConnection.Open "Active Directory Provider"
      Set objCommand.ActiveConnection = objConnection

      strQueryText = "<LDAP://" & searchRoot & ">;(&(objectCategory=Person)(samAccountName=" & strUserName &"));" _
            & "givenName,sn,mail,telephonenumber,ADsPath;SubTree"
      objCommand.CommandText = strQueryText
      objCommand.Properties("Page Size") = 2000
      objCommand.Properties("Timeout") = 60
      objCommand.Properties("Cache Results") = False
      Set objRecordSet = objCommand.Execute

      ' --- Loop through the returned records
      Do Until objRecordSet.EOF
            strFName = objRecordSet.Fields("givenName").value
            strLName = objRecordSet.Fields("sn").value
            strEmail = objRecordSet.Fields("mail").value
            strPhone = objRecordSet.Fields("telephonenumber").value

            'write field values to file
      objFile.WriteLine strUserName & "," & strLName & "," & strFName & "," & strEmail & "," & strPhone

Set objFile = Nothing
Set objFSO = Nothing
Set objUser = Nothing
Set objGroup = Nothing
Set objShell = Nothing
Wscript.Echo "Done.  Please check your Desktop for the output file"

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now