Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB Script Needed to query user information from AD

Posted on 2011-03-15
1
Medium Priority
?
443 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:seaninman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 28

Accepted Solution

by:
omgang earned 2000 total points
ID: 35138050
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
      objRecordSet.MoveFirst

      ' --- 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
            objRecordSet.MoveNext
      Loop

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


Next
objFile.Close
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"
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Loops Section Overview

596 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question